0

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

我试过下面的代码,但市场按钮的图像是重叠的,但价格标签工作正常,不重叠:

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

1 回答 1

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.

希望这可以帮助。

于 2013-08-23T02:44:34.530 回答