简体   繁体   中英

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

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.

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

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

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.

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.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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