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

时间:2013-08-23 02:07:36

标签: ios objective-c uitableview

我是新手。我想创建一个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;
}

1 个答案:

答案 0 :(得分:1)

我没有看到在单元格中添加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.

希望这有帮助。