2017-01-28 13 views
0

我一直在通過this tutorial工作,我可以得到單元格信息顯示,但只有當該特定單元格不在視圖中。例如,最下面的三個單元格會加載,因爲它們在「摺疊」下方,我必須滾動才能找到它們。一旦我向下滾動,頂部單元格就會出現。 objective-c新手,所以我甚至不確定哪裏開始。你能有人指出我正確的方向嗎?UIList視圖顯示單元格只有一次他們的視圖

What it looks like after scrolling down

#import "agendaController.h" 

@implementation agendaController{ 

    NSDictionary *schedule; 
    NSArray *scheduleSectionTitles; 

} 

- (IBAction)goBack:(UIStoryboardSegue *)segue{ 



} 

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    //Will be JSON from web 
    schedule = @{@"Monday, February 6th" : @[@"6:15 p.m. VIP ticket access", 
              @"6:30 p.m. Doors open", 
              @"7:00 p.m. General Session 1" 
              ], 
       @"Tuesday, February 7th" : @[ 
              @"9:30 a.m. VIP ticket access", 
              @"9:45 a.m. Doors open", 
              @"10 a.m. General Session 2", 
              @"6:15 p.m. VIP ticket access", 
              @"6:30 p.m. Doors open", 
              @"7:00 p.m. General Session 3" 
              ], 
       @"Wednesday, February 8th" : @[ 
              @"9:30 a.m. VIP ticket access", 
              @"9:45 a.m. Doors open", 
              @"10 a.m. General Session 4", 
              @"9:45 a.m. Doors open", 
              @"9:30 a.m. VIP ticket access", 
              @"7:00 p.m. General Session 5 (Baptisms immediately following service)" 
              ] 
       }; 

    scheduleSectionTitles = [[schedule allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 


} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return [scheduleSectionTitles count]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return [scheduleSectionTitles objectAtIndex:section]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    NSString *sectionTitle = [scheduleSectionTitles objectAtIndex:section]; 
    NSArray *sectionSchedule = [schedule objectForKey:sectionTitle]; 
    return [sectionSchedule count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    // Configure the cell... 
    NSString *sectionTitle = [scheduleSectionTitles objectAtIndex:indexPath.section]; 
    NSArray *sectionAnimals = [schedule objectForKey:sectionTitle]; 
    NSString *prepschedule = [sectionAnimals objectAtIndex:indexPath.row]; 
    cell.textLabel.text = prepschedule; 



    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 
    //Configure cell 
    return cell; 
} 


@end 

回答

1

我覺得你把小區創建代碼在錯誤的地方:

  • 當你調用dequeueReusableCellWithIdentifier,你將只能拿回細胞
    • ,你有創建之前和
    • 當前正在滾動的視圖(例如因爲低於底部/高於表格的頂部)。
  • 因此,第一些出隊呼叫將簡單地返回nil,因爲沒有任何出隊。
  • 現在您嘗試配置不存在的單元格並設置其文本。在目標C中,致電nil的呼叫(消息)不會崩潰,但不會執行任何操作。但是,這更難以調試。
  • 最後,您檢查單元格是否爲nil,如果,然後創建一個新單元並返回此新(未配置)的單元格。
  • 一旦你滾動,有細胞出隊(因爲他們已經滾出視圖),然後將被重用和配置,現在你開始看到你的數據。

的解決方案是重新你的代碼和dequeueReusableCellWithIdentifier後直接把細胞創作:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
} 
// Configure the cell... 
+0

完美。謝啦! –

0

可以註冊電池原型,並使用新的出列的方法來擺脫無核查。它將始終返回已正確調整大小的已出隊單元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    // Configure the cell... 
    NSString *sectionTitle = [scheduleSectionTitles objectAtIndex:indexPath.section]; 
    NSArray *sectionAnimals = [schedule objectForKey:sectionTitle]; 
    NSString *prepschedule = [sectionAnimals objectAtIndex:indexPath.row]; 
    cell.textLabel.text = prepschedule; 


    //Configure cell 
    return cell; 
} 
相關問題