简体   繁体   中英

How to add buttons and label in table cell dynamically?

I am developing an iphone application in that i have a requirement as showned in below template 在此处输入图片说明

you can add create UIButton and add them to cell as subView in cellForRowAtIndexPath : method. You can subClass UITableViewCell and in init method create a UIButton and add to subView. You can also create cell via xib and load them dynamically. A tutorial to customize UITableViewCell http://www.appcoda.com/ios-programming-customize-uitableview-storyboard/

This is totally depend upon your coding. You need to write the logic for the tableview cell in the method like :-

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

        UILabel *lblTeam = (UILabel*)[cell viewWithTag:321];
        UILabel *lblCRank = (UILabel*)[cell viewWithTag:258];
        UILabel *lblPRank = (UILabel*)[cell viewWithTag:369];

*// You can set the CGRectMake co-ordinates using variable like CGRectMake((x+12), (y+3), 120, 15); below  *

        if(!lblTeam){
            lblTeam = [[UILabel alloc]initWithFrame:CGRectMake(110, 52, 120, 15)];
            lblTeam.backgroundColor = [UIColor clearColor];
            lblTeam.textColor = [UIColor colorWithRed:103/255.0 green:103/255.0 blue:103/255.0 alpha:1.0];
            lblTeam.tag = 321;
            lblTeam.font = [UIFont fontWithName:@"Futura-Medium" size:12.0];
            [cell addSubview:lblTeam];
            [lblTeam release];

            lblCRank = [[UILabel alloc]initWithFrame:CGRectMake(110, 19, 150, 25)];
            lblCRank.backgroundColor = [UIColor clearColor];
            lblCRank.textColor = [UIColor colorWithRed:245/255.0 green:168/255.0 blue:0/255.0 alpha:1.0];
            lblCRank.tag = 258;
            lblCRank.font = [UIFont fontWithName:@"Futura-Medium" size:14.0];
            [cell addSubview:lblCRank];
            [lblCRank release];

            lblPRank = [[UILabel alloc]initWithFrame:CGRectMake(187, 20, 40, 25)];
            lblPRank.backgroundColor = [UIColor clearColor];
            lblPRank.textColor = [UIColor colorWithRed:245/255.0 green:168/255.0 blue:0/255.0 alpha:1.0];
            lblPRank.tag = 369;
            lblPRank.font = [UIFont fontWithName:@"Futura-Medium" size:12.0];
            [cell addSubview:lblPRank];
            [lblPRank release];

        }
        //Set the values of the labels or buttons here-
        [lblPRank setText:@""];
        [lblCRank setText:@""];
        lblTeam.text = [countryName uppercaseString];
        return  cell;
   }

If you still face any problem, show us your code which you are trying we can help you much better then.

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