ios - 寻找有关如何将数据填充到具有两个 uiview 的 uitableviewcell 中的解决方案

标签 ios

<分区>


想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题.

已关闭 9 年前

我创建了 UITableViewCell 的子类并添加了两个相同的 UIView具有完全相同的格式。

MyTableViewCell:

 UIView *leftView    = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, 160, 100)];
 UIView *rightView   = [[UIView alloc ] initWithFrame:CGRectMake(160, 0, 160, 100)]; 
 [self.contentView addSubview:leftView];
 [self.contentView addSubview:rightView];

每次我需要在 indexPath.row 时分配两个值表示仅一个索引。这是我当前的代码:

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

    if (indexPath.row %2) {
        cell.label1.text = [array  objectAtindex:indexPath.row]  objectForKey:@"name"];
        cell.label2.text =[array  objectAtindex:indexPath.row]  objectForKey:@"name"]; 
     }
}

单元格布局:

   |------------|--------------|
   |     1      |      2       |
   -----------------------------
   |     3      |      4       |
   -----------------------------
   |     5      |      6       |
   -----------------------------

结果是单元格 2、3、6 为空,单元格 1、4、5 已填充。我不知道如何将数据填充到两个相同的 UIView 中。

最佳答案

假设您的数组包含的项目数量恰好是表格行数的两倍,它应该像这样工作:

NSUInteger indexForField1 = indexPath.row * 2;
NSUInteger indexForField2 = indexPath.row * 2 + 1;
cell.label1.text = [array  objectAtindex:indexForField1]  objectForKey:@"name"];
cell.label2.text =[array  objectAtindex:indexForField2]  objectForKey:@"name"];

关于ios - 寻找有关如何将数据填充到具有两个 uiview 的 uitableviewcell 中的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9725089/

上一篇:iphone - 您能否将 TWTweetComposeViewController 的图像托管从 Twitter 更改为自定义内容?

下一篇:objective-c - iOS Messenger 的属性

相关文章:

javascript - 升级到 React Native 0.63.0 后 Pod 安装错误

ios - JSQMessages 的自定义单元格

ios - Sirikit与Flutter集成

ios - 是否实例化ViewControllerWithIdentifier : create a copy of the VC or will the storyboard one be magically not created

ios - 如何将 UIGestureRecognizerDelegate 分配给 CKComponentController

ios - 无法在 Collection View 中选择项目,Swift

ios - 如何在 TableView 或标签中提取和显示必要的数据

iphone - 如何在已有的PDF中绘图?

ios - 在 iOS 7 后台模式下有效跟踪用户的问题

ios - 应用重启后的本地通知