tableViewCell、collectionViewCell

目录

  • 注册重用法
  • tableview自适应高度
  • tableview撤销高亮
  • 参数的传递
    1. 纯代码(didselectRow)
    2. storyBoard方式(prepareForSegue)

tableViewCell、collectionViewCell重用注册法

  1. 纯代码注册时用registerClass如下图示例
  2. xib界面注册时用registerNib作(tableView或collectionView)的cell
  3. storyboard
  • 注册方法

1
2
3
4
5
6
[self.myCollectionView registerClass:[MycollectionViewCell class]
forCellWithReuseIdentifier:@"cell"];
[self.myCollectionView registerNib:[UINib nibWithNibName:@"mycollectionCell"
bundle:nil]
forCellWithReuseIdentifier:cellIndentify];
  • 重用方法

1
2
3
MycollectionViewCell *cell;
cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIndentify
forIndexPath:indexPath];

让tableViewCell横线没有空隙

1
2
3
cell.separatorInset = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;

tableview自适应高度

1
2
3
4
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}

tableView 编辑操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//编辑的状态
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}
//左滑时的题目
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"删除此诗";
}
//编辑状态下的操作
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPathP{
if (editingStyle == UITableViewCellEditingStyleDelete) {
}
}