2015-06-26 55 views
1

我有一個字符串數組,並希望我的UITableView中的每個部分使用各自的字符串作爲其行的標題。而不是這樣做,它只是取出數組中的第一個字符串,然後在每個部分重複它。UITableView重複每個部分的單元格中的相同字符串

我似乎無法弄清楚我在做什麼錯了設置。我有indexPath.sectionagendaTableArray值記錄,並且在滾動表格時它會正確記錄,但它仍然只顯示第一個值。

陣列設置:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

/// code clipped for brevity 

    self.agendaTableArray = [[NSArray alloc] init]; 

    self.agendaTableArray = @[@"No events today!", @"No events today!", @"Dinner with Rebekah", @"Soccer game", @"Dentist appt.", @"Celebrate job offer, drinks with Pierre!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!"]; 

} 

表設置:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *MyIdentifier = @"MyReuseIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier]; 
    } 

    NSLog(@"the indexpath.section is: %ld", (long)indexPath.section); 
    NSLog(@"the agendaTableArray title is: %@", self.agendaTableArray[indexPath.section]); 


    cell.textLabel.text = self.agendaTableArray[indexPath.row]; 
    cell.textLabel.font = [UIFont systemFontOfSize:14]; 

    [self whatSectionsAreVisible]; 

    return cell; 
} 
+0

請加上你的numberofRows和numberOfSection方法,好像你顛倒了數據源實現 – Doro

+0

這是不是你的[上一個問題]的重複(http://stackoverflow.com/questions/31059765/how-to-have - 每個字符串功能於一個陣列待其-自有的UITableViewCell-每個-在其通自己的)? – rmaddy

回答

3

你應該考慮檢查你每節多少行。爲了話,你可能會在返回值1 - (NSInteger)numberOfRowsInSection',因此你總是用你的第一行,也就是0

的索引你可能想使用:

cell.textLabel.text = self.agendaTableArray[indexPath.section]; 
0

我相信問題是以下行:

cell.textLabel.text = self.agendaTableArray[indexPath.section]; 

它應該是:

cell.textLabel.text = self.agendaTableArray[indexPath.row]; 

我假設你想要數組中的每個項目有一行,而不是陣列上每個項目的一個部分。

+0

咦?你的建議是OP已經做的。這就是問題所在。你的假設是不正確的。問題指出該表每節有一行。 – rmaddy

相關問題