我有一个 UILabel 作为我的单元格的 subview 。当用户突出显示一个单元格时,标签文本会改变它的颜色,而当该单元格未突出显示时,颜色会变回,就像普通的单元格文本标签一样。
问题是,如果用户点击一个单元格并滚动表格,该单元格将取消突出显示,取消突出显示委托(delegate)方法将被调用,但它将返回 [2147483647, 2147483647] 作为索引路径,并且文本不会更改,因为那里该索引路径上没有单元格。
代码如下:
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath*)indexPath{
NSInteger section = [indexPath section];
//only change the text on cells at section 1
if (section == 1) {
//get cell with given index path
UITableViewCell *currentCell = [tableView cellForRowAtIndexPath:indexPath];
// retrieve the UILabel by it's tag
UILabel *cellLabel = (UILabel *)[currentCell viewWithTag:1011];
cellLabel.textColor = [UIColor darkGrayColor];
}
}
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath*)indexPath{
NSInteger section = [indexPath section];
if (section == 1) {
UITableViewCell *currentCell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *cellLabel = (UILabel *)[currentCell viewWithTag:1011];
[cellLabel performSelector:@selector(setTextColor:) withObject:[UIColor lightGrayColor] afterDelay:0.2f];
}
}
当用户滚动表格并突出显示单元格时,我如何检索正确的单元格以更改它的 UILabel subview 文本?
最佳答案
测试这些方法时,我对 indexPath
得到了同样奇怪的结果(不知道,为什么这些方法不能正常工作)。
但我建议只编写您自己的 UITableViewCell
子类并覆盖以下方法(每次选择/取消选择单元格时都会调用该方法):
- (void) setSelected : (BOOL) selected animated : (BOOL) animated {
[super setSelected: selected animated: animated];
// your customisations to your UILabel
if (selected == NO) {
self.textLabel.textColor = [UIColor lightGrayColor];
}
}
希望这个回答能帮到你解决问题
关于iphone - 当用户滚动表格并突出显示单元格时,取消突出显示行在索引路径上返回最大整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17190754/