我的类如下:
头文件:
#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];
}
运行后提示错误如下:
2015-04-14 22:55:58.289 BmobDanmaku[4909:267989] dealloc
2015-04-14 22:55:58.296 BmobDanmaku[4909:267989] *** -[BmobBubbleDanmaku numberOfSectionsInTableView:]: message sent to deallocated instance 0x7d0bab20
也就是运行到numberOfSectionsInTableView时,我的bmobBubbleDanmaku已经被释放掉了,但是我不理解为什么会被释放了,求高手帮忙解答一下。