UITableview , dynamically section & rows

无人久伴 提交于 2020-01-11 12:11:25

问题


want create a uitableview dynamically with 2 rows in one section

i write this code but i have problem the repeats only 2 first rows in all sections

now i have this row 0,1 -> section 0 , row 0,1 -> section 1 , row 0,1 -> section 2

want read all rows after for example row 0,1 -> section 0 , row 2,3 -> section 1 , row 4,5 -> section 2 -> row 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;
}

回答1:


You have a problem in your mapping of indexPaths to the rows of your data structure (in this case, your items array). Each section will be numbered from 0 to ceil(n/2) (which, by the way, should be the number of sections, not simply n/2) where n is the number of elements in items.

Each row inside the section will have an indexPath.row of 0 or 1.

Therefore, in order to map from indexPaths back to your items array, you have to do something like this in your cellForRowAtIndexPath function:

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



来源:https://stackoverflow.com/questions/17765420/uitableview-dynamically-section-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!