我在一个 Controller 中有 2 个 UITableView。每个表格都有自己的单元格原型(prototype),带有不同的 UITableViewCell 子类:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == self.commentsTable){
CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentCell"];
cell.message.text = @"test";
}else if(tableView == self.paramsTableView){
RealtyParamCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RealtyParamCell"];
cell.label.text = @"test2";
}
return cell;
}
如果我这样做,最后一行会出现错误
return cell;
: “使用未定义的标识符单元”但我无法定义 cell
在一开始的时候:UItableViewCell *cell;
if(tableView == self.commentsTable){
<...>
}
因为我需要精确的单元格 -
CommentCell
或 RealtyParamCell
最佳答案
好吧,你仍然可以定义cell
开始时
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UItableViewCell *cell;
if(tableView == self.commentsTable){
CommentCell *commentCell= [tableView dequeueReusableCellWithIdentifier:@"CommentCell"];
commentCell.message.text = @"test";
cell = commentCell;
}else if(tableView == self.paramsTableView){
RealtyParamCell *realtyParamCell = [tableView dequeueReusableCellWithIdentifier:@"RealtyParamCell"];
realtyParamCell.label.text = @"test2";
cell = realtyParamCell;
}
return cell;
关于ios - 如何根据 "if"条件创建对象的不同子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23750696/