Iphone 在按下状态和选定状态下更改UITableViewCell的颜色
我正在使用customCell的UITableView(customCell有两个标签和一个ImageView) 在正常模式下,我需要所有单元格的白色 如果用户按下特定单元格,则该单元格的颜色应为灰色(其余单元格应为白色) 如果用户释放同一单元格,则该单元格的颜色应为橙色(其余单元格应为白色) 我怎么才能知道呢 我尝试过使用setSelectedBackground、willSelectRowAtIndexPath和手势方法。但无法看到同一单元格的这3种颜色状态。这两个州中的任何一个都在一起工作 你知道如何实现相同的功能吗 我使用选择器在android中实现了相同的功能。我希望iPhone具有相同的功能。有什么帮助吗 提前谢谢 如果你想要灰色,那么Iphone 在按下状态和选定状态下更改UITableViewCell的颜色,iphone,uitableview,android-selector,Iphone,Uitableview,Android Selector,我正在使用customCell的UITableView(customCell有两个标签和一个ImageView) 在正常模式下,我需要所有单元格的白色 如果用户按下特定单元格,则该单元格的颜色应为灰色(其余单元格应为白色) 如果用户释放同一单元格,则该单元格的颜色应为橙色(其余单元格应为白色) 我怎么才能知道呢 我尝试过使用setSelectedBackground、willSelectRowAtIndexPath和手势方法。但无法看到同一单元格的这3种颜色状态。这两个州中的任何一个都在一起工作
cell.selectionStyle=UITableViewCellSelectionStyleGray;
或者您可以在didSelectRow
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView reloadData];
UITableViewCell *cell=(UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor orangeColor]];
}
如果不想重新加载tableData,则必须保存以前选择的索引值,然后
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Make oreange cell
UITableViewCell *presentCell=(UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
[presentCell setBackgroundColor:[UIColor orangeColor]];
//make your previous cellBackgrod to clear, you can make it white as per your requirement
UITableViewCell *previouscell=(UITableViewCell*)[tableView cellForRowAtIndexPath:previousSelectedCellIndexPath];
[previouscell setBackgroundColor:[UIColor clearColor]];
//save your selected cell index
previousSelectedCellIndexPath=indexPath;
}
在自定义单元格中编写这两个方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
self.contentView.backgroundColor=[UIColor greenColor];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
self.contentView.backgroundColor=[UIColor orangeColor];
}
一个非常优雅的解决方案是覆盖tableViewCells的setHighlighted:方法
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if(highlighted) {
_backView.backgroundColor = [UIColor blueColor];
}
else
{
_backView.backgroundColor = [UIColor blackColor];
}
}
当用户点击所选单元格时,UITableView将自动将所选单元格的突出显示属性设置为“是”
别忘了调用
[\u tableView descellatindexpath:indepath-animated:NO]如果要取消选中单元格,请在didSelect tableView委托方法中选择代码> 如果你提供一点源代码会有帮助。你使用自定义单元格视图吗?@NANNAV是的,我在didSelectRowAtIndexPath中为单元格使用了自定义单元格背景色:但这只有在他点击该单元格时才会起作用,当他释放点击时,他会返回白色…@Rajneesh071它只会改变选择颜色。不是什么时候Pressed@Rajneesh071它正在工作,但唯一的问题是重新加载tableView会使表闪烁。但功能符合要求我正在从rss提要中检索表视图数据,并且由于数据很大,当我重新加载表视图时,它会显示一些刷新的flash之类的东西。但是,我正在尝试延迟加载imageview数据,这可能会解决您必须重新加载表格或上一个选定单元格才能删除背景色的问题…@AkbariDipali您可能需要调用[super-touchesend:touchs-withEvent:event]另外,请记住在touchesCancelledMethods不为我调用时使用highlighted=YES
设置适当的背景。iOS9模拟器