【发布时间】:2014-04-12 01:46:44
【问题描述】:
我正在尝试在 UITableView 上调用 selectRowAtIndexPath,该 UITableView 是我从中调用它的 UIViewController 的子视图。
我已经设置好了,当你选择一个单元格时,它会变成灰色,这很好,但是我正在将不同的数据集加载到 UITableView 中和从 UITableView 中加载出来,并且每当进行选择时,我都会将选定的 NSIndexPath 发送回UIViewController。然后,当视图下一次加载正确的 NSIndexPath 数据集时,我会从 UIViewController 调用此方法。
if (codeIndexPath != nil) {
[filterViewController.tableView selectRowAtIndexPath:codeIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}
然后在 UITableView 类中,我的 cellForRowAtIndexPath 和 didSelectRowAtIndexPath 看起来像这样。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *projectDescriptionString = [currentFilterMutableArray objectAtIndex:indexPath.row];
cell.textLabel.text = projectDescriptionString;
if (indexPath == currentlySelectedIndex) {
cell.highlighted = YES;
} else if (indexPath == currentlySelectedIndex) {
cell.highlighted = NO;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
// send this data back in the delegate so you can use it to show where the tick is again if you need too.
currentlySelectedIndex = indexPath;
[[self delegate] updateInstallTableWithFilter:[currentFilterMutableArray objectAtIndex:indexPath.row] FilterType:filterType InstallIndex:indexPath];
}
当它在屏幕上加载时,正确的单元格将突出显示一秒钟然后变回白色。
更新
cellForRowAtIndexPath 中的新 if 语句
if ([indexPath isEqual:currentlySelectedIndex]) {
cell.highlighted = YES;
} else if (![indexPath isEqual:currentlySelectedIndex]) {
cell.highlighted = NO;
}
我仍然收到同样的错误。
【问题讨论】:
-
您不应该将 indexPaths 与“==”进行比较,因为它们是对象。你应该使用 isEqual: 代替。
-
不仅 if 和 else if 都是 == 哈哈错过了那个.. 我更新了它.. 将在几秒钟内显示新代码。
标签: ios objective-c uitableview uiviewcontroller