UITableView实用技巧

1.去除多余的空白单元格,当UITableView的单元格较少,UITableView空白处会出现多余的单元格:

 self.tableView.tableFooterView=[[UIView alloc] initWithFrame:CGRectZero];

2.UITableViewCell设置选中状态时的视图:

    UIImageView *checkView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 1, 27, 27)];
    checkView.image    = [UIImage imageNamed:@"FlyElephant"];
    cell.accessoryView = checkView;

3.单元格选中无选中色:

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

4.设置分割线位置:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Remove seperator inset
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    
    // Explictly set your cell's layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

5.UITableView中设置tableFooterView之后,最后一行的分割线会被隐藏,可以通过下面的方法设置:

- (void)layoutSubviews {
    [super layoutSubviews];
    for (UIView *subview in self.contentView.superview.subviews) {
        NSString *subCls=NSStringFromClass(subview.class);
        if ([subCls hasSuffix:@"SeparatorView"]) {
            subview.hidden = NO;
        }
    }
}

你可能感兴趣的:(UITableView实用技巧)