Iphone uitableview每行显示3个对象

Iphone uitableview每行显示3个对象,iphone,objective-c,ios,uitableview,Iphone,Objective C,Ios,Uitableview,我有一个包含20个对象的数组。该数组中可以有更多对象。为了简单起见,让我们假设它是该数组中唯一的nsstring对象 我想在每一行中显示其中的3个元素。所以行的数量是 - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { int i = 0; if ( ([myArray count] % 3) > 0 ) { i++; } return [my

我有一个包含20个对象的数组。该数组中可以有更多对象。为了简单起见,让我们假设它是该数组中唯一的nsstring对象

我想在每一行中显示其中的3个元素。所以行的数量是

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
  int i = 0;

if ( ([myArray count] % 3) > 0 )
{
    i++;
}
return [myArray count] / 3 + i;
}

我有一个助手可验证
int lastObj=0

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

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
  ---instaniate the cell

   for (int i = 1; i <= 3; i++)
    {
        if (lastObj <= [myArray count])
        {
            --create cell content
            -- cellContent.myLabel.text=[myArray onbjectAtIndex:lastObj]
            --add cellContent to cell 
            lastObj++;

        }
    }
return cell;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil)
{
---使细胞瞬间化

对于(int i=1;i您尝试在其中插入3个文本标签的单元格中当前只有1个文本标签。您似乎很接近,但我认为最好的选择是创建一个包含三个文本标签的自定义表格单元格,然后按照您当前所做的操作来设置它们。以下是设置自定义选项卡单元格的教程:

尝试以下操作:

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
      ---instaniate the cell
      cell = [[UITableViewCell alloc] init]; //If you don't ARC then retain accordingly. 

    }

       for (int i = indexPath.row * 3 ; i < indexPath.row * 3 + 3; i++)
        {


            if (i <= [myArray count])
            {

                --create cell content
                -- cellContent.myLabel.text=[myArray onbjectAtIndex:i]
                --add cellContent to cell 
                //forget about your last object helper. That would not work anyway. 

            }
        }
    return cell;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil)
{
---使细胞瞬间化
cell=[[UITableViewCell alloc]init];//如果没有,则相应地保留。
}
对于(int i=indexPath.row*3;i如果(i
NSLog
out您的
lastObj
变量,并查看它在每个循环中设置了什么。它不超过12,它总是得到+1,我正在尝试理解您的解决方案。为什么要执行第3次(IndexPath.row*3)?您有20个对象,20/3=6(+2),因此表中需要6+1=7行。cellForRowAtIndexPath将被调用7次。每次需要从arrayindexPath读取3个值。row会告诉您调用方法的时间。这就是为什么必须将indexPath.row与3相乘的原因,因为需要从数组中读取3个对象。对于每一行,从数组中读取3次值:row+0,第+1行和第+2行是arrayDude的索引,您说过要“模拟3列”?只需实现3个部分,您就完成了您的代码实际显示并在第一行:obj0 obj1 obj2第二行:obj1 obj2 obj3。谢谢,但这看起来很有趣。您获得有关调用cellForRowAtIndexPath的序列、关于重用单元格的概念以及必须(重新)的信息非常重要每次使用该方法时,设置每个单元格的数据,无论该点上的单元格是否为nil。如果for循环在if语句之外,则可以看到每个单元格上的内容。否则,您的解决方案是第三个解决方案,显示前12个对象,然后从第一个对象开始循环-或用于设置t的内容的任何内容单元格-必须在if语句之外。如果在其他内容之上有内容,则解决相关的布局问题,而不是询问循环是否应在if语句之外。关键是-如果显示10个单元格-将重用第一个单元格以显示第11行。在这种情况下,单元格不是零(因为dequeueReusableCellWithIdentifier确实返回单元格)但是你还是必须分配第11行的数据/内容。哦,好的,谢谢你的澄清。现在我成功了。我使用了你的解决方案,因为它与我的解决方案很接近。谢谢!因为我在cell.contentview中添加了视图,我不得不删除视图。我不知道添加视图,然后从设备性能方面删除是否是个好主意总工程师。
    - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
      ---instaniate the cell
      cell = [[UITableViewCell alloc] init]; //If you don't ARC then retain accordingly. 

    }

       for (int i = indexPath.row * 3 ; i < indexPath.row * 3 + 3; i++)
        {


            if (i <= [myArray count])
            {

                --create cell content
                -- cellContent.myLabel.text=[myArray onbjectAtIndex:i]
                --add cellContent to cell 
                //forget about your last object helper. That would not work anyway. 

            }
        }
    return cell;
}