2016-04-25 181 views
-2

我想弄清楚,利弊在两个以下。两者都很好。UITableViewCell自定义

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath 
{ 
    UITableViewCell *tableViewCell = nil; 
    MyChildUITableViewCell *childTableViewCell = 
    (MyChildUITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"MyChildUITableViewCell"]; 

    tableViewCell = childTableViewCell; 
    return tableViewCell; 
} 

OR

- (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    MyChildUITableViewCell *childTableViewCell = nil; 

    childTableViewCell = (MyChildUITableViewCell*) 
    [tableView dequeueReusableCellWithIdentifier:@"MyChildUITableViewCell"]; 
    return childTableViewCell; 
} 
+4

问题标题与代码有什么关系? – Shubhank

回答

1

因为它是现在,这两个代码选项是相同的。如果这就是您要使用代码所做的一切,那么这两者都没有特别的赞成或反对意见。

如果您计划在表中使用不同类型的单元格,唯一可以考虑的选项是选项1。该代码将很好地设置以处理不同类型的单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath 
{ 
    UITableViewCell *tableViewCell = nil; 

    if (conditionsForCellOfType1) { 
     MyChildUITableViewCell *childTableViewCell = 
     (MyChildUITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"MyChildUITableViewCell"]; 

     tableViewCell = childTableViewCell; 
    } else { 
     DifferentTypeOfCell *differentCell = 
     (DifferentTypeOfCell*)[tableView dequeueReusableCellWithIdentifier:@"DifferentTypeOfCell"]; 

     tableViewCell = differentCell; 
    } 

    return tableViewCell; 
}