我與indexPath.section
從cellForRowAtIndexPath:
方法一個奇怪的問題。怪異的行爲「的cellForRowAtIndexPath:」
我有4段分組的實現代碼如下,我嘗試應用自定義UITableViewCell
爲第3,但它不工作。
當我嘗試if(indexPath.section==0){...}
它的工作(併爲section==1
和section==2
以及),但它失敗section==3
。 (?)
我不知道爲什麼,那是沒有意義的..是不是有人已經有了這樣(怪)的問題?
當我嘗試if(indexPath.row==0){...}
它適用於所有的4個部分..所以..?!
這裏是我的代碼:
//ViewController.h
import "DirectionsTableViewCell.h"
DirectionsTableViewCell *directionsCell; // customized UITableViewCell
//ViewController.m
if (indexPath.section==3) {
static NSString *CellIdentifier = @"directionsCell";
DirectionsTableViewCell *cell = (DirectionsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"DirectionsTableViewCell" owner:self options:nil];
cell = directionsCell;
}
return cell;
}
else {
static NSString *CellIdentifier = @"defaultCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = @"Test";
return cell;
}
問題解決了!
我只是說if(indexPath.row)
並能正常工作。
最後你得到這個:
if(indexPath.section==3) {
if(indexPath.row) {
static NSString *CellIdentifier = @"directionsCell";
DirectionsTableViewCell *cell = (DirectionsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"DirectionsTableViewCell" owner:self options:nil];
cell = directionsCell;
}
return cell;
}
}
在何種意義上失敗?你得到什麼錯誤或意外的結果? – PengOne 2012-01-12 23:13:48
我其實有這個問題。你可以發佈你的'cellForRowAtIndexPath'方法嗎?這可能有助於確認我的預感。 – 2012-01-12 23:16:01
好吧,我沒有任何錯誤,但它不加載自定義UITableViewCell ..我仍然有默認的。 – bs7 2012-01-12 23:16:59