UITableview,动态部分&行 [英] UITableview , dynamically section & rows

查看:99
本文介绍了UITableview,动态部分&行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要在一个部分中动态创建一个uitableview

want create a uitableview dynamically with 2 rows in one section

我写这段代码但我有问题重复所有部分只有2行

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

现在我有这一行0,1 - >第0部分,第0,1行 - >第1部分,第0行 - >第2部分

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

想要读取所有行,例如第0行 - >第0部分,第2,3行 - >第1部分,第4,5行 - >第2部分 - >第6行$ 7
项目= [[NSMutableArray alloc] init];

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;
}


推荐答案

你的问题在于将indexPaths映射到数据结构的行(在本例中为 items 数组)。每个部分的编号从0到ceil(n / 2)(顺便说一下,应该是部分的数量,而不仅仅是n / 2),其中n是项目中的元素数量

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.

该部分内的每一行都有一个 indexPath.row 为0或1 。

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

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

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])];

这篇关于UITableview,动态部分&行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆