1.用TableViewController在storyboard上创建控制器时,会自动连上dataSource和delegate,返回每组的行总数和单元格的明细是必须实现的方法,h文件里父类改为UITableViewController,storyboard右侧的class要重新选择,需要把Xcode重启后再选择(不重启看不到ViewController).
2.新建TableViewController后出现警告
unsupported configuration prototype table cells must have reuse identifiers
解决办法:图右侧的identifier是空的,table view cell属性面板中设置了identifier值,就会自动关闭此警告信息。
3.XIB注意事项
XIB的定义步骤
-----------------------------------------------
1> 新建TgCell.xib
2> 拽一个需要自定义的控件,摆放其他子控件
3> 新建一个类
* 类名要与XIB的名字保持一致
* 继承自的子类要与XIB中的根节点的类型一致
4> 要连线之前,需要将XIB的根节点类名修改为刚刚新建的类名
5> 连线
6> 在XIB的属性面板,指定可重用标示符
加载代码:
cell = [[[NSBundle mainBundle] loadNibNamed:@"TGCell" owner:nil options:nil] <span style="color:#ff0000;">lastObject</span>];
4.用类方法创建cell
代码创建Cell的步骤
-----------------------------------------------
1> 创建自定义Cell,继承自UITableViewCell
2> 根据需求,确定控件,并定义属性
3> 用getter方法完成控件的实例化,只创建并添加到contentView,不处理位置
4> 定义一个模型属性,通过setter方法,设置cell的显示
<span style="background-color: rgb(255, 255, 0);">-(void)setData:(tuanGou *)data{
//在setter方法中,第一句一定要赋值语句,否则在其他方法中使用模型将无法访问到
_data = data;
self.iconView.image = [UIImage imageNamed:data.icon];
self.titleLabel.text = data.title;
self.priceLabel.text = data.price;
self.buycountLabel.text = [NSString stringWithFormat:@"已购买人数%@",data.buyCount];
}</span>
5.延时
用dispatch_after
- (IBAction)loadBtn:(UIButton *)sender {
self.loadMore.hidden = YES;
self.loadingView.hidden = NO;
//延时,多线程GCD
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.loadingView.hidden = YES;
self.loadMore.hidden = NO;
});
}
6.tableFooterView只需要设置高度就可以了
//tableFooterView只需要指定高度就可以了,系统会设置了位置和宽度
// UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
// view.backgroundColor = [UIColor redColor];
// self.tableView.tableFooterView = view;
7.刷新数据
-(void)footerViewDidClickedBtn:(footerView *)footer{
NSLog(@"加载数据操作");
NSDictionary *dict = @{@"buyCount":@"100",@"icon":@"9b437cdfb3e3b542b5917ce2e9a74890",@"price":@"250",@"title":@"爆米花"};
tuanGou *newdata = [tuanGou dataWithDict:dict];
[self.dataList addObject:newdata];
//刷新数据,全部刷新数据
// [self.tableView reloadData];
//在末尾插入数据来刷新
//count-1是因为前面数据个数已经增加了1个,行数是从0开始,所以要减一。
NSIndexPath *path = [NSIndexPath indexPathForRow:(self.dataList.count - 1) inSection:0];
[self.tableView <span style="background-color: rgb(255, 255, 0);">insertRowsAtIndexPaths</span>:@[path] withRowAnimation:UITableViewRowAnimationAutomatic];
}
8.判断代理是否实现协议方法,若是没有判断,当协议方法是optional可选性的,是不会报错的,所以先判断,避免了未具体实现的协议方法的使用。
//先判断代理是否实现的方法
if (<span style="background-color: rgb(255, 255, 0);">[self.delgete respondsToSelector:@selector(footerViewDidClickedBtn:)]</span>) {
[self.delgete footerViewDidClickedBtn:self];
}
9.开发小技巧
使用if 0
#if 0
需要不执行的代码
#end if
改为if 1,就可以代码被执行。这样测试时,就可以不需要到注释一大段了。
10.自定义代理
代理模式:父控件(控制器)监听子控件(自定义控件)的事件,当子控件发生某些事情时,通知父控件工作。
子控件--->父控件去工作 使用代理;
父控件--->子控件去工作,直接调用方法即可。
View:
1>协议 protocol,声明协议方法
2>添加一个遵守协议的delegate属性
代理属性如果使用强引用,就会产生循环引用,(控制器,自定义控件,dele属性),因为自定义控件放在控制器里,形成强引用,所以使用弱引用避免引用循环,造成控制器和子视图都无法释放,内存泄露。
Controller:
1>遵守协议;
2>使控制器称为View的代理
3>具体的协议方法实现
11.关于单元格背景颜色
self.backgroundColor -- 绿色 (这个tableView的背景色)
self.contentView.backgoundColor -- 红色(这才是单元格背景色)
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected) {
// self.backgroundColor = [UIColor redColor];
self.contentView.backgroundColor = [UIColor redColor];
}else{
self.backgroundColor = [UIColor greenColor];
}
// Configure the view for the selected state
}