如何管理一节中的行数?

时间:2011-07-11 11:42:26

标签: iphone

我在其中一个应用程序中使用表视图。在那个数量的部分是一个,行数是20.我想在每一行放置图像和两个标签。因此,为了填充行,我在CellForrowIndexpath方法

中编写了以下代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      static NSString *CellIdentifier = @"Cell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
          cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
      }
      int i=indexPath.row;
      if (indexPath.row==i)
      {

          cell = [self getCellContentView:CellIdentifier];

          UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
          UILabel *lblTemp2 = (UILabel *)[cell viewWithTag:2];
          //UILabel *lblTemp3 = (UILabel *)[tableCell viewWithTag:3];
           lblTemp1.text = [ctrycodearray objectAtIndex:indexPath.row];
           lblTemp2.text = @"Alert";

          UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0,0, 100, 30)];
          img.image=[UIImage imageNamed:@"thumb_mdpi.png"];

          //NSString *cellValue1 = [default1 stringForKey:@"keyToRepeatString"];
          //repeatlbl.text=cellValue1;   

          //[img release];
        [cell.contentView addSubview:img];

          return cell;
      }

getCellContentView方法中,我声明了两个标签。执行完这个后,我得到了错误。这个cellForRowAtIndexPath方法执行了6到7次并打印了6行或7行>但是如果移动到发生在下面的错误。所以请告诉我如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

最好将自定义UITableViewCell用于这类事情。

参考:Apple doc

试试这个doc&如果你有任何问题,请。发布在这里。

编辑:不需要阅读整个文档。只需查看该链接中的Listing 5-3 Adding subviews to a cell’s content view即可。

相关问题