2017-04-13 43 views

回答

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

這是UITableView的委託方法。返回的對象是UITableViewCell類型的。這些是您在表格行中看到的對象。 NSIndexPath有兩個這個Section和Row。

如果在視圖控制器中實現UITableViewDataSource協議,則會調用它。更簡單的方法是添加一個UITableViewController類。我強烈建議這樣做,因爲它爲您編寫了一些代碼,以便輕鬆實現可以描述表格的函數。無論如何,如果你選擇自己實現這個協議,你需要創建一個UITableViewCell對象並將其返回給任何行。看看它的類的引用來理解可重用性,因爲在表視圖中顯示的單元格被一次又一次地重複使用(這是一個非常有效的設計)。

如果您實現自定義單元格,然後我會強烈建議您使用

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

只返回這裏沒有設置空單元格。用東西一樣

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

    CartTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCart_Cell" forIndexPath:indexPath]; 
return cell; 
} 

,並使用該委託只是cellForRow數據源的方法之後,會叫

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


     if ([cell isKindOfClass:[CartTableViewCell class]]) 
    { 
     CartTableViewCell *cell1 = (CartTableViewCell*)cell; 
     MyCartModel* data = [_myCartProductArray objectAtIndex:indexPath.row]; 
     [cell1 setUpData:data]; 
    } 

} 

和UITableViewCell的自定義類設置的UILabel數據。