在-tableView:cellForRowAtIndexPath:
设置selectionStyle
为UITableViewCellSelectionStyleNone
或者您在界面生成器中执行此操作(目标是避免选择蓝色/或灰色)
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SomeCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Configure your cell
return cell;
}
然后在-tableView:didSelectRowAtIndexPath:
自定义行为:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *useCurrentLocationCellIndexPath = [NSIndexPath indexPathForRow:1
inSection:0];
NSIndexPath *pickCityCellIndexPath = [NSIndexPath indexPathForRow:2
inSection:0];
if ([indexPath compare:useCurrentLocationCellIndexPath] == NSOrderedSame) {
// The "use current location" cell was selected. Change the image to the highlighted image
[tableView cellForRowAtIndexPath:indexPath].imageView.image = highlightedImage;
} else if ([indexPath compare:pickCityCellIndexPath] == NSOrderedSame) {
// The "pick city" cell was selected. Change the image to normal one. And show the picker using your code.
[tableView cellForRowAtIndexPath:useCurrentLocationCellIndexPath].imageView.image = normalImage;
//[self showCityPicker];
}
}