如何动态地在表格单元格中添加按钮和标签?

时间:2013-11-11 12:37:48

标签: ios iphone ios7

我正在开发一个iphone应用程序,因为我有一个要求,如下面的模板enter image description here所示

2 个答案:

答案 0 :(得分:0)

您可以添加创建UIButton并将其添加到subView:方法中cellForRowAtIndexPath的单元格中。 你可以subClass UITableViewCell并在init方法中创建一个UIButton并添加到subView。 您还可以通过xib创建单元格并动态加载它们。  自定义UITableViewCell http://www.appcoda.com/ios-programming-customize-uitableview-storyboard/

的教程

答案 1 :(得分:0)

这完全取决于您的编码。您需要在方法中编写tableview单元格的逻辑,如: -

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

如果您仍然遇到任何问题,请告诉我们您正在尝试的代码,我们可以为您提供更好的帮助。