2013-08-30 17 views
1

我通過爲主tableview創建自定義單元格,在tableview的每個單元格中都有一個tableview。問題是我可以看到除第一個之外的所有索引路徑的tableview內容。如果我滾動tableview使第一個單元格出來,並滾動第一個單元格,第一個單元格中的內容顯示。在indexpath零的項目不會顯示,直到indexpath 0滾出屏幕

請找我的代碼如下

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [itemArray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *[email protected]"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
     cell.textLabel.numberOfLines = 0; 
     cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0]; 
    } 
    for (int i=0; i<[itemArray count]; i++) { 
     ////NSLog(@"row is %d",i); 
     if (indexPath.row==i) { 
      cell.textLabel.text=[itemArray objectAtIndex:i]; 
     } 
    } 
    return cell; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

誰能告訴我,爲什麼它正在發生。我想要在屏幕上看到第一個單元格的內容。

+1

這幾乎是不可能回答'UITableView'沒有看到你的'的tableView的問題:的cellForRowAtIndexPath:'方法。 – dasblinkenlight

+0

和numberOfRowsInSection – Radu

+1

@dasblinkenlight請檢查我的代碼 – surendher

回答

0

請更新下面的方法。它可能會幫助你。並且請確保您在itemArray中添加對象後重新加載tableview。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 
// add a placeholder cell while waiting on table data 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; 
    } 
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
    cell.textLabel.numberOfLines = 1; 
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0]; 
    cell.textLabel.text=[[[DataManager GetInstance]getCurrentTableContent] objectAtIndex:indexPath.row]; 
} 

return cell; 
+0

在重新加載數據之前,我有一個表數據。我想知道如何解決這個問題,如果我向下滾動,然後第一個單元格的內容顯示在屏幕上 – surendher

+0

通過編寫我的方法它不會得到解決? –

+0

是的,它沒有解決 – surendher

1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [itemArray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *[email protected]"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
     cell.textLabel.numberOfLines = 0; 
     cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0]; 
    } 
    // why are you using this code in for loop ?? 
    /* 
    for (int i=0; i<[itemArray count]; i++) { 
     ////NSLog(@"row is %d",i); 
     if (indexPath.row==i) { 
      cell.textLabel.text=[itemArray objectAtIndex:i]; 
     } 
    } 
    */ 
    cell.textLabel.text=[itemArray objectAtIndex:indexPath.row]; 
    return cell; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
}