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

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

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

请帮我解决这个问题。

最佳答案

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

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

感谢大家的快速周转并提供宝贵的建议以完成它。

关于ios - 在每个单元格上显示 JSON 值后,在最后一个单元格的 UITableViewCell 中动态添加两个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34491693/

相关文章:

ios - IOS 应用程序中的硬编码

objective-c - 继续检查直到 True

iphone - SQLite数据库未在iPhone设备上加载

ios - 如何在 Objective-C 中将任何字符串文本转换为纯文本格式

ios - 从标签中删除缩进

iphone - 使用 CGRect 显示的图像遮盖了 UIButtons

ios - 检测相机的主动使用

ios - Firebase 云消息传递适用于控制台,但不适用于 FCM api

objective-c - UILongPressGestureRecognizer 不适用于 tableview

iOS ressizedImageWithCapInsets & @2x、@3x 图像