1. 消除plain樣式的多余的(沒有數(shù)據(jù)的)cell
self.tableView.tableFooterView = [UIView new];
2. 異步加載cell的內(nèi)容, 優(yōu)化tableView的性能
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID forIndexPath:indexPath];
if (!cell) {
cell = [CustomCell cell];
}
// 在此處進行數(shù)據(jù)異步加載
[self asyncLoadingCellWithHandler:^(CustomModel *result) {
dispatch_async(dispatch_get_main_queue(), ^{
cell.model = result;
});
}];
return cell;
}
3. cell的插入/刪除/刷新/移動
// 需要用到的相關方法
- (void)beginUpdates;
- (void)endUpdates;
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection NS_AVAILABLE_IOS(5_0);
- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath NS_AVAILABLE_IOS(5_0);
// 獲取當前cell所在的組數(shù)
NSUInteger sectionNum = indexPath.section;
// 查看當前數(shù)據(jù)所在的組中的行數(shù)
NSUInteger rowsCount = [self.tableView numberOfRowsInSection:sectionNum];
// 刷新數(shù)據(jù)源: 增加數(shù)據(jù)/刪除數(shù)據(jù)/修改數(shù)據(jù)/移動數(shù)據(jù)
...
// 開始執(zhí)行UI操作
[self.tableView beginUpdates];
例:
1. 刪除cell
/*
如果操作的tableView是plain樣式, 在刪除cell的時候需要考慮到當前組中是否只有一個cell,
如果是, 需要在執(zhí)行 deleteRowsAtIndexPaths:withRowAnimation: 方法的時候同時執(zhí)行 deleteSections:withRowAnimation: 否則會一直報錯
*/
if (rowsCount == 1) {
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];
[self.tableView deleteSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
}
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
2. 插入cell
...
3. 更新cell
...
4. 移動cell
...
[self.tableView endUpdates];
4. plain樣式的組頭標題會自動懸停, group樣式的組頭標題不會自動懸停
在此方法中設置組頭標題內(nèi)容
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
5. 自定義左滑cell, 出現(xiàn)編輯按鈕的樣式
iOS 11以前, 在自定義cell的- (void)layoutSubviews方法中自定義左滑cell后的編輯按鈕樣式
- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *subV in self.subviews.firstObject.subviews) {
if ([subV isKindOfClass:NSClassFromString(@"_UITableViewCellActionButton")]) {
UILabel *buttonLabel = subV.subviews.firstObject;// UIButtonLabel
[buttonLabel removeFromSuperview];
// 添加文字
NSString *titleStr = buttonLabel.text;
CGSize titleSize = [titleStr boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{
NSFontAttributeName:[UIFont systemFontOfSize:12] } context:nil].size;
CGFloat imageWH = 18;
CGFloat topAndBottomMargin = 14;
CGFloat labelX = (CGRectGetWidth(subV.frame) - titleSize.width) * 0.5;
CGFloat labelY = CGRectGetHeight(subV.frame) - (topAndBottomMargin + titleSize.height);
CATextLayer *titleLayer = [CATextLayer layer];
titleLayer.contentsScale = 2;
titleLayer.frame = CGRectMake(labelX, labelY, titleSize.width, titleSize.height);
titleLayer.string = titleStr;
titleLayer.fontSize = 12;
titleLayer.foregroundColor = [UIColor whiteColor].CGColor;
[subV.layer addSublayer:titleLayer];
// 添加圖片
CGFloat imageX = (CGRectGetWidth(subV.frame) - imageWH) * 0.5;
CGFloat imageY = topAndBottomMargin;
CGRect imageRect = CGRectMake(imageX, imageY, imageWH, imageWH);
UIImage *image = nil;
if ([titleStr isEqualToString:@"刪除"]) {
image = [UIImage imageNamed:@"delete_icon"];
}
if ([titleStr isEqualToString:@"發(fā)送"]) {
image = [UIImage imageNamed:@"send_icon"];
}
CALayer *layer = [CALayer layer];
layer.frame = imageRect;
layer.contents = (id)image.CGImage;
[subV.layer addSublayer:layer];
}
}
}
iOS 11以后, 在tableView的- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath中自定義左滑cell后的編輯按鈕樣式
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
for (UIView *subV in self.tableView.subviews) {
if ([subV isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) {
for (UIButton *actionBtn in subV.subviews) {
if ([actionBtn isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
// 添加文字
NSString *titleStr = actionBtn.titleLabel.text;
CGSize titleSize = [titleStr boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{
NSFontAttributeName:[UIFont systemFontOfSize:12] } context:nil].size;
UILabel *actionTitleLabel = actionBtn.titleLabel;
CGFloat topMargin = 14;
CGFloat bottomMargin = topMargin;
CGFloat imageWH = 18;
CGFloat labelX = CGRectGetMinX(actionTitleLabel.frame) + (CGRectGetWidth(actionTitleLabel.frame) - titleSize.width) * 0.5;
CGFloat labelY = bottomMargin + imageWH;
CATextLayer *titleLayer = [CATextLayer layer];
titleLayer.contentsScale = 2;
titleLayer.frame = CGRectMake(labelX, labelY, titleSize.width, titleSize.height);
titleLayer.string = titleStr;
titleLayer.fontSize = 12;
titleLayer.foregroundColor = [UIColor whiteColor].CGColor;
[actionBtn.layer addSublayer:titleLayer];
// 添加圖片
CGFloat imageX = CGRectGetMinX(actionTitleLabel.frame) + (CGRectGetWidth(actionTitleLabel.frame) - imageWH) * 0.5;
CGFloat imageY = topMargin;
CGRect imageRect = CGRectMake(imageX, imageY, imageWH, imageWH);
UIImage *image = nil;
if ([titleStr isEqualToString:@"刪除"]) {
image = [UIImage imageNamed:@"delete_icon"];
}
if ([titleStr isEqualToString:@"發(fā)送"]) {
image = [UIImage imageNamed:@"send_icon"];
}
CALayer *layer = [CALayer layer];
layer.frame = imageRect;
layer.contents = (id)image.CGImage;
[actionBtn.layer addSublayer:layer];
// 移除系統(tǒng)文字
[actionTitleLabel removeFromSuperview];
}
}
}
}
}