通过 xib 中的标识符选择自定义 UITableViewCell

发布于 2024-09-14 09:39:31 字数 897 浏览 5 评论 0原文

我尝试用不同的 UITableViewCell 填充我的 UITableView。我尝试通过标识符从 XIB 文件中选择 3 个 UITableViewCell 中的 1 个,但无法获取 UITableViewCell 标识符的值。

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



MyTableViewCell *cell = (MyTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"CellTwo"];

 if(nil == cell){

  NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil];
  for (id currentObject in topLevelObjects) {
   if ([currentObject isKindOfClass:[MyTableViewCell class]]) {
    MyTableViewCell *tmpCell = (MyTableViewCell *) currentObject;
    if (tmpCell.identifier == @"CellTwo") {
     cell = (MyTableViewCell *) currentObject;     
    }
    [tmpCell release];
    break;
   }
  } 
 }

 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

 return cell;}

I try to fill my UITableView withd different UITableViewCells. I tried to choose 1 of 3 UITableViewCells out of a XIB-file by identifier but I can't get the value of the UITableViewCell's Identifier.

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



MyTableViewCell *cell = (MyTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"CellTwo"];

 if(nil == cell){

  NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil];
  for (id currentObject in topLevelObjects) {
   if ([currentObject isKindOfClass:[MyTableViewCell class]]) {
    MyTableViewCell *tmpCell = (MyTableViewCell *) currentObject;
    if (tmpCell.identifier == @"CellTwo") {
     cell = (MyTableViewCell *) currentObject;     
    }
    [tmpCell release];
    break;
   }
  } 
 }

 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

 return cell;}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莫言歌 2024-09-21 09:39:31

我通过为每个 UITableViewCell 创建一个子类解决了这个问题。当您对类类型进行比较时,您可以输入单元格的类型:

        cell = (FooterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"footerTableViewCell"];

        if (cell == nil){

            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CommonTableViewCell" owner:nil options:nil];

            for (id currentObject in topLevelObjects){
                if ([currentObject isKindOfClass:[FooterTableViewCell class]]){
                    cell = (FooterTableViewCell *)currentObject;
                    return cell;
                }
            }
        }

我有一些表格重复使用完全相同类型的单元格格式,并且我希望将它们放在一个可以随时更改的通用 XIB 中。在该控制器的 .h 中,我简单地为三种单元格类型中的每一种声明了 UITableViewCell 的三个子类。

I solved this exact problem by creating a subclass for each UITableViewCell. When you do your comparison of the class type, you can put in the type of cell it is:

        cell = (FooterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"footerTableViewCell"];

        if (cell == nil){

            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CommonTableViewCell" owner:nil options:nil];

            for (id currentObject in topLevelObjects){
                if ([currentObject isKindOfClass:[FooterTableViewCell class]]){
                    cell = (FooterTableViewCell *)currentObject;
                    return cell;
                }
            }
        }

I have some tables that reuse the exact same kind of cell formatting and i wanted to have them in a common XIB that i can alter at anytime. In the .h for that controller, i simply declared three sub classes of UITableViewCell for each of the three cell types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文