在每个单元格上显示JSON值后,在UITableViewCell中在最后一个单元格中动态添加两个按钮

时间:2015-12-28 10:23:08

标签: ios objective-c iphone uitableview

我需要一个IPhone应用程序,我在其中处理JSON字符串,从中转换对象,然后将此对象的值显示到UITableView中的每个单元格。 JSON字符串可以变化,因此没有。细胞都可以变化。

我的要求是显示两个按钮"接受"和"拒绝"在显示JSON值后的下两个单元格中。

问题是我找不到实现它的方法。

这是我一直在尝试的但是没有帮助: -

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

static NSString *CellIdentifier = @"Cell";

TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil) {
    cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

NSInteger lastSectionIndex = [tableView numberOfSections] - 1;
NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1;
NSIndexPath *pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];if(row==(pathToLastRow.row+1)){
    UIButton *acceptButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    acceptButton.frame = CGRectMake(0.0f, 5.0f, 400.0f, 44.0f);
    acceptButton.backgroundColor = [UIColor redColor];
    [acceptButton setTitle:@"ACCEPT" forState:UIControlStateNormal];
    [cell addSubview:acceptButton];
}
if(row==(pathToLastRow.row+2))
{
    UIButton *declinedButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    declinedButton.frame = CGRectMake(0.0f, 5.0f, 400.0f, 44.0f);
    declinedButton.backgroundColor = [UIColor redColor];
    [declinedButton setTitle:@"DECLINED" forState:UIControlStateNormal];
    [cell addSubview:declinedButton];
}    NSString *values = [valueArray objectAtIndex:indexPath.row];
    cell.valueObtained.text = values;
NSString *titles = [titleArray objectAtIndex:indexPath.row];
    cell.headerInfo.text = titles;

return cell;
}

它不会在单元格中显示按钮。

请帮我解决这个问题。

4 个答案:

答案 0 :(得分:1)

在这里,我按照上面的建议更改了代码,以便将按钮设置为页脚,并且可以看到它显示正常。

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [valueArray count];
 }

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

    static NSString *CellIdentifier = @"Cell";
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if(cell == nil) {
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }


    NSString *values = [valueArray objectAtIndex:indexPath.row];
        cell.valueObtained.text = values;


        NSString *titles = [titleArray objectAtIndex:indexPath.row];
        cell.headerInfo.text = titles;

    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    {
        return 100.0f;
    }

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{

    UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];

        UIButton *rejectButton=[UIButton buttonWithType:UIButtonTypeCustom];
        rejectButton.frame=CGRectMake(0, 0, 130, 30);
        [rejectButton setTitle:@"Reject" forState:UIControlStateNormal];
        [rejectButton addTarget:self action:@selector(rejectButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [rejectButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        rejectButton.tag = 1;

        UIButton *approveButton=[UIButton buttonWithType:UIButtonTypeCustom];
        approveButton.frame=CGRectMake(200, 0, 130, 30);
        [approveButton setTitle:@"Approve" forState:UIControlStateNormal];
        [approveButton addTarget:self action:@selector(approveButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [approveButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

        [footerView addSubview:rejectButton];
        [footerView addSubview:approveButton];
        return footerView;

}

- (void)rejectButtonPressed:(id)sender
{
    NSLog(@"Reject Button Pressed");
}

- (void)approveButtonPressed:(id)sender
{
    NSLog(@"Approved Button Pressed");
}

谢谢大家的快速周转,并提供有价值的建议来完成它。

答案 1 :(得分:0)

而不是将这些按钮添加为表格的单元格。您可以随时将它们添加为表格的超级视图的表格页脚或子视图(使它们成为视图的顶部)。

您可以使用以下代理配置表格的页脚:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {}

答案 2 :(得分:0)

仅更改此内容:

UIButton *acceptButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 acceptButton.frame = CGRectMake(0.0f, 5.0f, 400.0f, 44.0f);
 acceptButton.backgroundColor = [UIColor redColor];
 [acceptButton setTitle:@"ACCEPT" forState:UIControlStateNormal];
 [cell.contentView addSubview:acceptButton]; // Change Here 

答案 3 :(得分:0)

您还可以在viewDidLoad方法

中将这些按钮添加到表格页脚视图中

UIView * fullView = [[UIView alloc] initWithFrame:CGRectMake(xPos,yPos,width,height)];     fullView.backgroundColor = [UIColor whiteColor];

self.tableView.tableFooterView = fullView;

UIButton * acceptButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

acceptButton.frame = CGRectMake(0.0f, 5.0f, 400.0f, 44.0f);

acceptButton.backgroundColor = [UIColor redColor];

[acceptButton setTitle:@"ACCEPT" forState:UIControlStateNormal];

[fullView addSubview:acceptButton];

UIButton * rejectedButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

declinedButton.frame = CGRectMake(0.0f, acceptButton.frame.size.height+10.0f, 400.0f, 44.0f);
declinedButton.backgroundColor = [UIColor redColor];
[declinedButton setTitle:@"DECLINED" forState:UIControlStateNormal];
[fullView addSubview:declinedButton];