【问题标题】:How to make UIButton appear in last cell only?如何使 UIButton 仅出现在最后一个单元格中?
【发布时间】:2014-07-04 04:39:26
【问题描述】:

我是 Objective C 编程的新手,并且有一个带有自定义单元格的 UITableView 设置。我想让它让用户可以触摸一个按钮来添加另一个单元格,并且这个按钮只会出现在最后一个单元格中。目前,它没有出现。这是我正在使用的代码。我在自定义单元格中创建了按钮,并使用“setHidden:YES”将其隐藏在单元格本身中。我正在尝试“setHidden:NO”使按钮出现在 TableView 代码中,但它不起作用。我想也许这与重新加载单元有关,但我不确定我是否朝着正确的方向前进。我将不胜感激任何帮助,谢谢。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{workoutTableViewCell *cell = (workoutTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell...
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell.addButton setTitle:(NSString *)indexPath forState:UIControlStateApplication];
[cell.textLabel setText:[NSString stringWithFormat:@"Row %i in Section %i", [indexPath row], [indexPath section]]];

NSInteger sectionsAmount = [tableView numberOfSections];
NSInteger rowsAmount = [tableView numberOfRowsInSection:[indexPath section]];

if ([indexPath section] == sectionsAmount - 1 && [indexPath row] == rowsAmount - 1) {
    NSLog(@"Reached last cell");
    [cell.addButton setHidden:NO];
    if (lc == NO)
    {[[self tableView] reloadData];
        lc = YES;
    }
}

 return cell;
 }

【问题讨论】:

  • 为什么要在cellForRowAtIndexpath 中重新加载表?
  • 最好的方法是将你的按钮添加到表格页脚视图。

标签: ios objective-c uitableview uibutton


【解决方案1】:

遵循UITableViewDataSource 方法将帮助您返回部分中可用的确切行数。在这里你需要返回额外的,因为你希望最后一个作为你的按钮。

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

现在在下面的方法中,您将使用 indexpath.row 作为检查行号

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *lastCellIdentifier = @"LastCellIdentifier";
    static NSString *workoutCellIdentifier = @"WorkoutCellIdentifier";

    if(indexPath.row==(yourRowCount+1)){ //This is last cell so create normal cell
        UITableViewCell *lastcell = [tableView dequeueReusableCellWithIdentifier:lastCellIdentifier];
        if(!lastcell){
            lastcell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:lastCellIdentifier];
            CGRect frame = CGRectMake(0,0,320,40);
            UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [aButton addTarget:self action:@selector(btnAddRowTapped:) forControlEvents:UIControlEventTouchUpInside];
            aButton.frame = frame;
            [lastcell addSubview:aButton];
        }
        return lastcell;
    } else { //This is normal cells so create your worktouttablecell
        workoutTableViewCell *cell = (workoutTableViewCell *)[tableView dequeueReusableCellWithIdentifier:workoutCellIdentifier];
        //Configure your cell

    }
}

或者您可以像@student 在评论代码中建议的那样以编程方式创建 UIView 并将其设置为 FooterView,

CGRect frame = CGRectMake(0,0,320,40);
UIView *footerView = [[UIView alloc] initWithFrame:frame];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton addTarget:self action:@selector(btnAddRowTapped:) forControlEvents:UIControlEventTouchUpInside];
aButton.frame = frame;
[footerView addSubView:aButton];
[yourTableNmae setTableFooterView:footerView];

声明方法如下

-(IBAction)btnAddRowTapped:(id)sender{
    NSLog(@"Your button tapped");
}

【讨论】:

  • 请在您的答案中编辑 cellForRowAtIndexPath 方法。对他的帮助更大。因为他做了错误的单元分配并且没有实现其他条件。谢谢@Janak
【解决方案2】:
if ([indexPath section] == sectionsAmount - 1 && [indexPath row] == rowsAmount - 1) {
    NSLog(@"Reached last cell");
    [cell.addButton setHidden:NO];
} else { 
    [cell.addButton setHidden:YES];
}

在您的程序中替换此代码。

【讨论】:

    【解决方案3】:

    如果您知道 uitable 中的单元格数量,并且只想知道最后一行何时出现,您可以实现以下委托方法

    • (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

    此方法告诉委托表视图即将为特定行绘制单元格,只需将您的行与表行数进行比较。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      • 2012-08-23
      • 2017-08-05
      • 2011-01-10
      相关资源
      最近更新 更多