2011-10-02 67 views
0

的幫助爲什麼indexPath.Row總是返回0 ???在下面的方法中需要NSIndexPath

- (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] autorelease]; 
     } 

    // Configure the cell. 
    [cell.textLabel setText:[mainItems objectAtIndex:indexPath.row]]; 
    return cell; 
} 

mainItems是NSMutableArray的,有5個項目( 「項目1」, 「項目2」,...)的View也獲得5項......但他們都 「項目1」,而不是項目1,第2項...

我做錯了什麼?

回答

3

我的猜測是你在-numberOfSectionsInTableView的實現中返回了錯誤的值,並且表視圖要求你輸入5個部分的第0行。

表格視圖有部分,每個部分都有行。許多表視圖只有一個部分,但該部分中有許多行。這聽起來像你有一個部分,5行。確保你在每一個返回正確的值:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
+0

tks很多...事實上,我設置部分= [mainItens計數],而不是項目!我會盡快標記爲正確的! – Leonardo