繁体   English   中英

如何在UITableview iOS的每一行中添加多个按钮和标签

[英]How to add multiple button and label in each row on UITableview iOS

我是新手。 我想创建一个UITableview,每行有2个按钮和3个标签。 我想加载tableview时,它将检查每个按钮的状态,然后为它们设置图像。 用户单击另一个选项卡栏中的其他按钮后,将重新加载uitableview并将图像设置为其状态。 我怎样才能做到这一点? 提前致谢。

我在下面的代码中尝试过,但是marketButton的图像是重叠的,但是Price标签可以正常工作,而不是重叠:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell *)[_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


        UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 0, 80, 30)];
        pricelabel.backgroundColor = [UIColor clearColor];
        pricelabel.font = [UIFont fontWithName:@"Helvetica" size:16];
        pricelabel.font = [UIFont boldSystemFontOfSize:16];
        pricelabel.textColor = [UIColor darkGrayColor];
        pricelabel.tag = 3000;
        //pricelabel.hidden = YES;

        pricelabel.textAlignment = NSTextAlignmentRight;
        [cell.contentView addSubview: pricelabel];
        [pricelabel release];

    }

    UIButton *marketButton = [UIButton buttonWithType:UIButtonTypeCustom];
     [market addTarget:self action:@selector(marketPressed:) forControlEvents:UIControlEventTouchDown];
[marketButton setTag:indexPath.row];

    if([sellingArray count]>0)
    {
    NSLog(@"sellingArray %@",sellingArray);
    if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]) // nothing
    {

        [marketButton setSelected:NO];
        [marketButton setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal];
        marketButton.enabled = YES;

    }
    else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"])  // marketplace
    {
        [marketButton setSelected:YES];
        [marketButton setImage:[UIImage imageNamed:@"MarketplaceSelect.png"] forState:UIControlStateNormal];
        marketButton.enabled = YES;

    }
    }

    if([priceNewArray count]> 0)
    {
        UILabel *pricelbl = (UILabel*)[cell.contentView viewWithTag:3000];
        pricelbl.text =[NSString stringWithFormat:@"$%@",[priceNewArray objectAtIndex:indexPath.row]];
        if ([sellingArray count]>0) {
            if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"]){

                pricelbl.hidden = NO;
            }
            else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]){
                pricelbl.hidden = YES;

            }

        }
    }

    return cell;
}

我没有看到在单元格中添加marketButton的代码。

基本上,当调用tableView reloadData时,它将调用cellForRowAtIndexPath并且由于您正在使用reusableidentifier ,它将返回一个已经具有标签和按钮的重用单元格。 因此,如果您再次创建按钮实例,那么它将重叠。

无论如何,执行此操作的基本思想是像现在一样在cell==nil块中创建标签和按钮,并为其分配标签。 cell==nil块之外,使用标签从单元中获取控件,然后根据需要更新控件。

所以代码的逻辑流程将像这样

if (nil == cell) {

    //Create new cell instance with reusable identifier
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:CellIdentifier];

    //Create the controls and assign tag
    UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 0, 80, 30)];
    pricelabel.tag = 3000;
    [cell.contentView addSubview: pricelabel];
    [pricelabel release];

    UIButton *marketButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [market addTarget:self action:@selector(marketPressed:)  forControlEvents:UIControlEventTouchDown];
    [marketButton setTag:3001];
    [cell.contentView addSubview: marketButton ];
}
//In the block outside, you can get the controls from the cell and update it.
UILabel *priceLbl = (UILabel*)[cell.contentView viewWithTag:3000];
UIButton*marketBtn = (UIButton*)[cell.contentView viewWithTag:3001];

//Add the code for updating the button based on some status.

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM