2017-01-12 23 views
0

自2009年以來,這是我填充的tableView細胞(自定義單元格)方式:正確的方法來填充的TableView細胞

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    AccountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccountCellID"]; 
    if (cell==nil) { 
     cell = [[AccountTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AccountCellID"]; 
    } 

// here update content 
    cell.customImg = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.png",indexPath.row]] 


    return cell; 
} 

這是好事還是有更好的辦法嗎?有人說我應該更新TableViewcell自定義類中的所有內容。有什麼區別嗎?

回答

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return Array.count; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    FilterTableViewCell *cell = (FilterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"filter cell"]; 
     BrandObjectClass *brandObject = [brandsArray objectAtIndex:indexPath.row]; 

     cell.lblFilterName.text = brandObject.brandName; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.lblFilterName.font = [UIFont fontWithName:@"Roboto-Medium" size:cell.lblFilterName.font.pointSize]; 
      cell.imgTick.hidden = NO; 


     return cell; 
} 

當您使用自定義表視圖的工作細胞鑄件也很重要,因爲dequeueReusableCellWithIdentifier回報UITableViewCell對象。

希望這會有所幫助。

+0

感謝那個鑄造的好點。但是細胞數據羣體呢?如果我們在cellForRow中或單元格的自定義類本身(即在CustomCell.m/h中)執行它,這有什麼關係? – GeneCode

+0

那麼,如果你在單元格的自定義類中執行它,那麼它將是MVC結構的完美實現。我沒有在我的系統上,所以我就這麼回答了這個錯誤的案例。 –