【问题标题】:Set UITableViewCells to different UITableViews in one ViewController在一个 ViewController 中将 UITableViewCells 设置为不同的 UITableViews
【发布时间】:2013-04-21 21:40:59
【问题描述】:

我用 iPhone 的 TableView 构建了一个 Viewcontroller。现在我尝试生成相同的内容,但按某种网格排序。它应该是 3 x 3。我生成了尺寸为 220px x 220px 的四边形,并将我的 TableView 的宽度设置为 220。

为了生成网格,我考虑再插入两个 Tableview,这将获得 3 个指定的单元格。问题只是,我的 ViewController 中有方法“cellForRowAtIndex”,我无法将单元格插入到我想要的 tableview 中。 我找到了一个方法,叫做

[self.channelTableView2 insertRowsAtIndexPaths:0    
                              withRowAnimation:UITableViewRowAnimationAutomatic];

但这似乎是在您处于编辑模式时向 tableview 添加新行的方法。

我还考虑过创建一个条件,为每个 tableview 设置 3 个特定列,但对我来说,cellForRowAtIndexPath 方法似乎只适用于一个 TableView,您不能在其中指定任何内容。

有人知道如何解决这个问题吗?我还看到了其他创建网格的帖子,但如果我创建新视图而不是新 UITableViewCell,则无法使用旧代码。我听说过 Collection View,但它只支持 iOS 6 的设备。

【问题讨论】:

    标签: uitableview grid cell nsindexpath


    【解决方案1】:

    我解决了这个问题。我有 3 个表格视图。他们的委托和数据源与我的 ViewController 连接。比我为这三个表创建了一个属性。在我的 cellForRowAtIndexPath 方法中,我检测到哪个 tableviewcell 将显示在特定的 tableview 中

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyObject *anObject;
    if(tableView == tableview1){
    
        anObject = [initializedObjects objectAtIndex:indexPath.section * 3];
    
    }else if(tableView == tableview2){
    
        anObject = [initializedObjects objectAtIndex:indexPath.section * 3 + 1];
    }else if(tableView == tableview3){
    
        anObject = [initializedObjects objectAtIndex:indexPath.section * 3 + 2];
    }
    

    我还禁用了所有表格的滚动,所以它似乎是一个网格。必须在“didSelectRowAtIndexPath”中进行相同的检测,否则您将只选择对象数组中的前三个元素。 (在本例中为“initializedObjects”)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多