开发者社区> 问答> 正文

在UITableView中添加数组首尾处的UIbutoton

问题:我要在UITableView中添加UIButton,并且只加在第一个和最后一个数组上,不知道应该怎么实现?我知道可以用tableFooterView添加按钮,但是不知道怎么样实现添加到指定数组位置中。请多多指教,谢谢

我的代码:

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    //Adding a UIButton in last row

    NSInteger lastSectionIndex = [editTable numberOfSections] - 1;
    NSLog(@"lastSectionIndex:%d",lastSectionIndex);

    // Then grab the number of rows in the last section
    NSInteger lastRowIndex = [editTable numberOfRowsInSection:lastSectionIndex] - 1;
    NSLog(@"lastRowIndex:%d",lastRowIndex);

    // Now just construct the index path
    NSIndexPath *pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
    NSLog(@"last index:%@",pathToLastRow);

    if (pathToLastRow.row == lastRowIndex) 
    {
        NSLog(@"row enters");

        checkButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [checkButton1 setFrame:CGRectMake(200, 0, 168, 168)];
        [checkButton1 addTarget:self
                         action:@selector(customActionPressed:)
               forControlEvents:UIControlEventTouchDown];
        [checkButton1 setBackgroundImage:[[UIImage imageNamed:@"Up Arrow.jpg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
        editTable.tableFooterView = checkButton1;
        [cell addSubview:checkButton1]; 
    }

现在我可以在tableview的每个单元里面接受到Button。

展开
收起
爵霸 2016-03-26 10:12:04 1908 0
1 条回答
写回答
取消 提交回答
  • 先修改if条件句,

    if ((lastSectionIndex == indexPath.section && lastRowIndex == indexPath.row ) || (indexPath.section == 0 && indexPath.row == 0 ))
    {

    然后就会这样:

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            UIButton *checkButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
            checkButton1.tag = 100;//not recommended, I would suggest to use custom UITableViewCell class and add this button as subview inside its init method
            [checkButton1 setFrame:CGRectMake(200, 0, 168, 168)];
            [checkButton1 addTarget:self
                         action:@selector(customActionPressed:)
               forControlEvents:UIControlEventTouchDown];
            [checkButton1 setBackgroundImage:[[UIImage imageNamed:@"Up Arrow.jpg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
            [cell addSubview:checkButton1]; 
        }
    
       //Adding a UIButton in last row
       NSInteger lastSectionIndex = [editTable numberOfSections] - 1;
       NSLog(@"lastSectionIndex:%d",lastSectionIndex);
    
       // Then grab the number of rows in the last section
       NSInteger lastRowIndex = [editTable numberOfRowsInSection:lastSectionIndex] - 1;
       NSLog(@"lastRowIndex:%d",lastRowIndex);
    
       // Now just construct the index path
       NSIndexPath *pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
       NSLog(@"last index:%@",pathToLastRow);
    
       UIButton *checkButton1 = (UIButton *)[cell viewWithTag:100];//not recommended, I would suggest to use custom UITableViewCell class and add this button as subview inside its init method
       if ((lastSectionIndex == indexPath.section && lastRowIndex == indexPath.row ) || (indexPath.section == 0 && indexPath.row == 0 ))
        {
           checkButton1.hidden = NO;
        } else {
           checkButton1.hidden = YES;
        }
    2019-07-17 19:15:31
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载