2011-11-20 147 views
12

我正在使用字典數組填充表視圖。數組和字典的內容被解析沒有問題。但是,該表中填充了[數組數量]的單元格,其內容索引爲0。在我看來,indexPath.row爲所有單元格返回0。我如何使用相應的索引填充每個單元格?indexPath.row始終爲0

- (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... 
NSDictionary *term = [self.terms objectAtIndex:indexPath.row]; 
cell.textLabel.text = [term objectForKey:@"content"]; 
cell.detailTextLabel.text = [term objectForKey:@"created"]; 

return cell; 
} 

非常感謝!

編輯:這是我得到的日誌記錄indexPath

2011-11-21 03:07:58.025演示[21269:F803 2個 指標[0,0]

2011-11 -21 03:07:58.027演示[21269:F803] 2個 索引[1,0]

好像所述第一索引更新,而第二不是。

當我登錄indexPath.row,然而

2011-11-21 03:19:40.306演示[21546:F803] 0

2011-11-21 03:19:40.308演示[21546:f803] 0

那麼,我應該使用什麼來獲得遞增的值?

再次感謝您的幫助。

+2

使用'indexPath.section' –

回答

36

該代碼塊本身看起來很好。也許你的tableview有多個部分各有一行?你要返回什麼數據源方法tableView:numberOfRowsInSection:(應該是[self.terms count] )和numberOfSectionsInTableView:(應該是1)。

如果這些都可以,將NSLog(@"%@",indexPath);放在方法的某處並將輸出發佈到您的問題中。

+0

這裏就是我有記錄indexPath: 2011-11-21 03:07:58.025演示[21269:F803 2個索引[0,0] 2011-11-21 03 :07:58.027演示[21269:f803] 2個索引[1,0] 我只有一個部分到我的表,我正在返回正確的值'tableView:numberOfRowsInSection:'和'numberOfSectionsInTableView:' –

+3

@SimonLee - 你看到的日誌記錄說表格視圖是要求第0部分,第0行,然後第1部分,第0行。所以我不認爲你的行和部分方法有正確的實現 - 你可以包括那些這個問題? – jrturton

+4

@jrturton感謝您的領導!原來,我在行中的段和段中實現了行。我切換後一切正常。愚蠢的錯誤! –

11

我剛剛有一個類似的問題(indexPath.row總是0),並注意到我有多少白癡。在我的numberOfSectionsInTableView我回來了[dataSet count]和在我的tableView:numberOfRowsInSection我回來了1.應該是相反的。哎呀!

+0

我也是...這是大聲笑! – Dmitry

+0

不是唯一的白癡:)。謝謝 –