UITableview,dynamic部分和行

要在一段中dynamic创build一个可用的视图

我写这个代码,但我有问题的重复只有2所有部分的第一行

现在我有这一行0,1 – > 0节,0,1行 – > 1节,0,1节 – > 2节

想要读取所有的行,例如行0,1 – > 0,2,3行 – >第1行,4,5行 – >第2行 – >行6,7 items = [[NSMutableArray alloc] init];

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return items.count/2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 2; } - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell; UILabel *label = nil; cell = [tv dequeueReusableCellWithIdentifier:@"Cell"]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease]; label = [[UILabel alloc] initWithFrame:CGRectZero]; [label setLineBreakMode:UILineBreakModeWordWrap]; [label setMinimumFontSize:FONT_SIZE]; [label setNumberOfLines:0]; [label setFont:[UIFont systemFontOfSize:FONT_SIZE]]; [label setTag:1]; label.textColor = [UIColor darkGrayColor]; [label setAlpha:0.8]; cell.backgroundColor = [UIColor whiteColor]; [[cell contentView] addSubview:label]; } NSString *text = [items objectAtIndex:[indexPath row]]; CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; if (!label) label = (UILabel*)[cell viewWithTag:1]; [label setText:text]; [label setFrame:CGRectMake(CELL_CONTENT_MARGIN-5, CELL_CONTENT_MARGIN+30, CELL_CONTENT_WIDTH - ((CELL_CONTENT_MARGIN * 2)+6), MAX(size.height, 44.0f))]; label.backgroundColor = [UIColor clearColor]; cell.selectionStyle = UITableViewCellSelectionStyleNone; for (UIView *theSubview in [cell.contentView subviews]) { if([theSubview isKindOfClass:[UIImageView class]]) { [theSubview removeFromSuperview]; } } UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(128,5, 32, 32)]; imv.image=[UIImage imageNamed:@"0England.png"]; [cell.contentView addSubview:imv]; [imv release]; if (indexPath.row % 2==0) { UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(-7 ,10, 8, 18)]; img.image=[UIImage imageNamed:@"...."]; [cell.contentView addSubview:img]; [img release]; } else { } return cell; } 

在将indexPath映射到数据结构的行(在本例中为items数组)时,您遇到了一些问题。 每个部分的编号从0到ceil(n / 2)(顺便提一句,应该是部分的数目,而不是简单的n / 2),其中n是items中元素的数量。

该部分中的每一行都将有一个0或1的indexPath.row

因此,为了从indexPaths映射回你的items数组,你必须在你的cellForRowAtIndexPath函数中做这样的事情:

NSString *text = [items objectAtIndex:([indexPath section]*2 + [indexPath row])];