在cellForRowAtIndexPath
写下面的代码:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CustomCellIdentifier = @"yourcell";
SettingsCell *cell = (SettingsCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell == nil) {
NSArray *nib = nib = [[NSBundle mainBundle] loadNibNamed:@"yourcell" owner:self options:nil];
for(id oneObject in nib) {
if([oneObject isKindOfClass:[yourCell class]]) {
cell = (yourCell *)oneObject;
}
}
//to change background color of selected cell
UIView *backgroundView = [[UIView alloc] init];
backgroundView.backgroundColor = [UIColor blueColor];
cell.selectedBackgroundView = backgroundView;
}
return cell;
}
在 indexPath 的 didselectRow 上,更改为另一种颜色
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView reloadData];
UITableViewCell *cell=(UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor redColor]];
}