Ios UITableview,动态剖面&;排

Ios UITableview,动态剖面&;排,ios,uitableview,nsmutablearray,Ios,Uitableview,Nsmutablearray,想要动态创建一个uitableview,在一个部分中包含两行吗 我写了这段代码,但我有一个问题,即在所有部分中只重复前两行 现在我有了第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 *)ta

想要动态创建一个uitableview,在一个部分中包含两行吗

我写了这段代码,但我有一个问题,即在所有部分中只重复前两行

现在我有了第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映射到数据结构的行(在本例中是
数组)时出现问题。每个部分将从0到ceil(n/2)进行编号(顺便说一句,这应该是部分的数量,而不仅仅是n/2),其中n是
项中的元素数量

节中的每一行都将有一个0或1的
indexPath.row

因此,为了从indexPaths映射回
数组,您必须在
cellForRowAtIndexPath
函数中执行以下操作:


NSString*text=[items objectAtIndex:([indexPath节]*2+[indexPath行]);

您可以将您的cellForRowAtIndexPath功能发布吗?问题可能是在您的indexPath->items[index]映射中。为什么要在另一个用户名下发布相同的问题(20分钟前发布的问题)?@shadow,每个部分都有从0开始的行。它不像第0节的行从零开始,第1节的行从1开始。如果这就是你想要的。我更新了这个问题我想为两行创建一个部分一个偶数行一个奇数行一个部分没有任何变化我想问题是//tableView NumberOfRowsInSection为什么会出现?你说你在每个部分都会重复两行,对吗?
numberofrowsinssection
方法只告诉表视图一个节中有多少行,而不是哪些行。如果您尝试过此代码,但它不起作用,请更新您的问题,并提供有关您看到的行为的更多详细信息。请勿重复!!我想在部分中有唯一的行现在我有第0,1行->第0节,第0,1行->第1节-----我想这一行是第0,1行->第0节,第2,3行->第1节,第4,5行->第2节是的,我想我明白你想做的。这就是我在这里介绍的代码的作用。