<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">因项目需求,需要将项目中所有tableView的列表做成便签效果,若在每个页面分别修改下类太过麻烦</span>
<span style="font-size:18px;">- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath</span>
好在这些页面都集成于一个UITableView父类,于是在父类中加入如下代码:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor clearColor];
UIImage *cellImage;
if (indexPath.row == 0) {
cellImage = [UIImage imageNamed:@"列表背景_上.png"];
}else if (indexPath.row == [self.tableView numberOfRowsInSection:0]-1){
cellImage = [UIImage imageNamed:@"列表背景_下.png"];
}else {
cellImage = [UIImage imageNamed:@"列表背景_中.png"];
}
UIImageView *cellImageView = [[UIImageView alloc]initWithFrame:cell.frame];
[cellImageView setImage:cellImage];
cell.backgroundView = cellImageView;
}
并在父类中修改
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;