我想調用selectRowAtIndexPath對UITableView是我調用它的UIViewController的子視圖。selectRowAtIndexPath從另一個UIVIewController不起作用
我已經設置好了,所以當你選擇一個單元格時,它會灰色,這很好,但是我加載了不同的數據集進出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];
}
在加載屏幕上的正確的單元格將突出第二再回去白色..
任何幫助,將不勝感激。
UPDATE
新的,如果語句內的的cellForRowAtIndexPath
if ([indexPath isEqual:currentlySelectedIndex]) {
cell.highlighted = YES;
} else if (![indexPath isEqual:currentlySelectedIndex]) {
cell.highlighted = NO;
}
我仍然reciving同樣的錯誤。
你不應該用「==」比較indexPaths,因爲他們的對象。您應該使用isEqual:來代替。 – rdelmar
不僅如果和如果都是==大聲笑錯過了一個..我更新了..會在一秒內顯示新的代碼。 – HurkNburkS