从一个充满字符串的数组中设置表视图中按钮的标题标签

时间:2014-09-11 05:56:55

标签: ios objective-c uitableview

我在视图控制器中创建了这个表视图。表视图将有一组按钮。现在我有一个包含这些按钮标签的数组,但按钮标签只设置为数组中的第一个对象。

如何将标签设置为数组中各自的对象,即第二个按钮将第二个对象作为标签,依此类推?这就是我一直在做的事情。

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"celllIdentifier";
    UIButton *btnProblem;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        btnProblem = [UIButton buttonWithType:UIButtonTypeCustom];
        btnProblem = [[UIButton alloc] initWithFrame:CGRectMake(100,0,635,75)];

       [btnProblem setBackgroundImage:[UIImage imageNamed:@"sc123_button_hover.png"] forState:UIControlStateSelected];
       [btnProblem setBackgroundImage:[UIImage imageNamed:@"sc123_button.png"] forState:UIControlStateNormal];

        UIFont *font = [UIFont fontWithName:@"RobotoSlab-Regular" size:25];
        //[btnProblem setTitle:[btnLabels objectAtIndex:indexPath.row] forState:UIControlStateNormal];

        [btnProblem.titleLabel setFont:font];
        btnProblem.tag = 1000;
        [cell addSubview:btnProblem];
        cell.backgroundColor =  [UIColor clearColor];
    }

    btnProblem  = (UIButton*)[cell viewWithTag:1000];
    return cell;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [_btnNext addTarget:self action:@selector(btnNextTapped:) forControlEvents:UIControlEventTouchUpInside];
    [_btnBack addTarget:self action:@selector(btnBackTapped:) forControlEvents:UIControlEventTouchUpInside];
    btnLabels = [[NSArray alloc] initWithObjects:@"Lime-scale on fixtures",@"Spots on dishes",@"Clothing becomes gray after washing",@"Clothing feels stiff or wrinkles easily in wash",@"Rings form in toilet or where water stands",@"Misting system clogs", @"Faucets don’t have even spray",@"Valves have white deposits at connections", nil];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView setBackgroundView:nil];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [btnLabels count];
}

1 个答案:

答案 0 :(得分:0)

试试这个 -

[btnProblem setTitle:[btnLabels[indexPath.section] forState:UIControlStateNormal];

而不是 -

[btnProblem setTitle:[btnLabels[indexPath.row] forState:UIControlStateNormal];

由于每个部分只有一行,访问数组的对象使用部分编号。 p.s还取消注释上面的代码行。

相关问题