首页 新闻 会员 周边

iOS内存释放问题

0
悬赏园豆:10 [待解决问题]

我的类如下:

头文件:

#import "BmobDanmaku.h"

@interface BmobBubbleDanmaku : BmobDanmaku
@property NSMutableArray *listData;
@property UITableView *tableView;
@property UITableViewCell *tableViewCell;
-(instancetype)init;

@end

实现文件:

//
//  BmobBubbleDanmaku.m
//  BmobDanmaku
//
//  Created by limao on 15/4/14.
//  Copyright (c) 2015年 Bmob. All rights reserved.
//

#import "BmobBubbleDanmaku.h"
@interface BmobBubbleDanmaku()<UITableViewDataSource,UITableViewDelegate>


@end

@implementation BmobBubbleDanmaku

@synthesize listData=_listData;

@synthesize tableView = _tableView;

@synthesize tableViewCell =_tableViewCell;

-(instancetype)init{
    if (self = [super init]) {
        //初始化表格
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 200, 200) style:UITableViewStylePlain];
        // 设置协议,意思就是UITableView类的方法交给了tabView这个对象,让完去完成表格的一些设置操作
        self.tableView.delegate=self;
        self.tableView.dataSource=self;
        self.listData = [[NSMutableArray alloc] initWithCapacity:0];
        [self addSubview:self.tableView];
    }
    return self;
}

//返回多少个section
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.listData count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    声明静态字符串型对象,用来标记重用单元格
    static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
    //    用TableSampleIdentifier表示需要重用的单元
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];
    //    如果如果没有多余单元,则需要创建新的单元
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:TableSampleIdentifier];
    }
    
    else {
        while ([cell.contentView.subviews lastObject ]!=nil) {
            [(UIView*)[cell.contentView.subviews lastObject]removeFromSuperview];
        }
    }
    
    //    获取当前行信息值
    NSUInteger row = [indexPath row];
    //    填充行的详细内容
    cell.detailTextLabel.text = @"详细内容";
    //    把数组中的值赋给单元格显示出来
    cell.textLabel.text=[self.listData objectAtIndex:row];
    
    
    //    cell.textLabel.backgroundColor= [UIColor greenColor];
    
    //    表视图单元提供的UILabel属性,设置字体大小
    cell.textLabel.font = [UIFont boldSystemFontOfSize:40.0f];

    //    设置单元格UILabel属性背景颜色
    cell.textLabel.backgroundColor=[UIColor clearColor];
    return cell;
}

-(void)dealloc{
    NSLog(@"dealloc");
}

@end


调用的代码如下:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"张三",@"张四",@"张五",@"李三",@"李四",@"李五",@"李六",@"王三",@"王四",@"王五",@"王六",@"王七",@"王八",@"王九",@"王十", nil];
    BmobBubbleDanmaku *bmobBubbleDanmaku = [[BmobBubbleDanmaku alloc] init];
    bmobBubbleDanmaku.listData = array;
    [self.view addSubview:bmobBubbleDanmaku.tableView];
}

运行后提示错误如下:

   也就是运行到numberOfSectionsInTableView时,我的bmobBubbleDanmaku已经被释放掉了,但是我不理解为什么会被释放了,求高手帮忙解答一下。

 
ios
狸猫副园长的主页 狸猫副园长 | 初学一级 | 园豆:192
提问于:2015-04-14 22:56
< >
分享
所有回答(2)
0

出了作用域,对象被释放了,应该生命为property

畅游者 | 园豆:204 (菜鸟二级) | 2015-09-04 20:42
0

你重新alloc了一块新的内存,BmobBubbleDanmaku *bmobBubbleDanmaku = [[BmobBubbleDanmaku alloc] init];,然后去找上一个内存中的TableView当然找不到了。错误的意思并不是,你这块内存释放掉了,而是你想找的那块内存的对象释放掉了。[self.view addSubview:bmobBubbleDanmaku.tableView];

逆时光 | 园豆:142 (初学一级) | 2015-09-10 10:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册