在单个tableview单元格中显示两个数组值

时间:2015-06-03 12:50:07

标签: ios uitableview

我想在tableview中为每个单元格显示webservies数组值我需要显示两个值Ex:总共十个值表示每个单元格在每一行中显示2个值。 webservies总值九表示每个显示两个值,最后一个单元格显示一个值。我怎样才能实现这一点帮助我。新的发展。

4 个答案:

答案 0 :(得分:1)

按照本教程的说明自定义单元格并按照您的需要进行设计:

Crafting Custom UITableView Cells

这是您可以为每个数组设置自定义单元格标签的值的方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    return cell;
}

答案 1 :(得分:1)

创建一个自定义类UITableViewCellCustomCell并为其添加2个标签,占用一半的空间或根据您的设计所需的内容。现在说它们是labelOnelabelTwo。 在Controller类中,您获得了需要以标签显示的对象数组。在UITableViewDataSource方法中使用此代码

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
      return (dataArray.count+1)/2;  //This will provide correct row count for odd data set, such as when count is 9
}

并使用此代码填充单元格标签文本

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
      CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if(cell==nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"customCell"];
    }
      cell.lableOne.text = [dataArray objectAtIndex:indexPath.row*2]; 
      if((indexPath.row*2)+1 < dataArray.count){ 
         cell.lableTwo.text = [dataArray objectAtIndex:(indexPath.row*2)+1];
      }

      return cell;  
 }

答案 2 :(得分:0)

使用%获取表视图的行数。然后对于最后一个单元格,如果value为null,则显示空白...您可以有一个基本逻辑。每个单元格中取2磅,并显示在其中包含值的标签上。

答案 3 :(得分:0)

cellForRow:中,您应该在[yourArray objectAtIndex:(indexPath.row * 2)][yourArray objectAtIndex:(indexPath.row * 2 + 1)]向数组发送数组中的值。所以第6行将获得索引12和13的对象。 您还应该始终检查对象是否存在。类似于 - if (yourArray.count > (indexPath.row * 2))if (yourArray.count > (indexPath.row * 2 + 1)),而不是发送到其他单元格。 (当你得到奇数个值时,你会在单元格中得到一个对象。