如何在 UITableview iOS 的每一行中添加多个按钮和标签 [英] How to add multiple button and label in each row on UITableview iOS

查看:47
本文介绍了如何在 UITableview iOS 的每一行中添加多个按钮和标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm a newbie. I want to create a UITableview which has 2 button and 3 label in each row. And i want to when load tableview, it'll check state of each button, then set image for them. After user click on other button in another tabbar, uitableview will be reloaded and set image as stats of them. How can i do that? Thanks in advance.

我尝试了下面的代码,但 marketButton 的图像是重叠的,但价格标签工作正常,而不是重叠:

I tried below code but image of marketButton is overlap but Price label works fine, not overlap:

- (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 的代码.

I didnt see the code for adding marketButton in the cell.

基本上,当 tableView reloadData 被调用时,它会调用 cellForRowAtIndexPath 并且由于您使用的是 reusableidentifier,它将返回一个重用的单元格已经有了标签和按钮.所以如果你再次创建按钮实例,那么它会重叠.

Basically, when tableView reloadData is called, it will call cellForRowAtIndexPath and since you are using reusableidentifier, it will return a reused cell which already have the label and button. So if you are creating the button instance again, then it will overlap.

无论如何,这样做的基本思想是,像现在一样在 cell==nil 块中创建标签和按钮,并为它们分配一个标签.在 cell==nil 块之外,使用标签从单元格获取控件并根据需要更新控件.

Anyways, the basic idea to do this is, create the labels and buttons inside cell==nil block as you have created like now and assign a tag to them. Outside the cell==nil block, get the control from the cell using tag and update the control as you wish.

所以代码的逻辑流程会是这样

So the logical flow of code will be like this

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.

希望这会有所帮助.

这篇关于如何在 UITableview iOS 的每一行中添加多个按钮和标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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