【问题标题】:Obj-C - Delete tableview row if button in custon tableview cell is tappedObj-C - 点击自定义表格视图单元格中的删除表格视图行按钮
【发布时间】:2017-11-04 17:49:35
【问题描述】:

我的自定义表格视图单元格中有一个复选标记按钮。在我的表格视图中点击该按钮时,我希望删除该行。

现在,如果点击单元格本身,我下面的代码会删除该行,但如果点击单元格内的按钮则不会。我怎样才能做到这一点,如果点击单元格内的按钮,该行将被删除?

ViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (tableView == self.sidetableView) {

        NSMutableDictionary *nodeData = [[self.myFriendData objectAtIndex:indexPath.row] mutableCopy];

        NSString *nid = [nodeData objectForKey:@"nid"];
        [nodeData setObject:nid forKey:@"nid"];

        [self.myFriendData removeObjectAtIndex:indexPath.row];

        [DIOSNode nodeDelete:nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) {

            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

            NSLog(@"node deleted!");
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"could not delete node!");
        }];


    }

CustomTableViewCell.m

-(void)accepted {

    NSString *friendAccepted = @"friendAccepted";
    [[NSUserDefaults standardUserDefaults] setObject:friendAccepted forKey:@"friendStatus"];
    [[NSUserDefaults standardUserDefaults] synchronize];

}

-(void)alreadyAccepted {

    NSString *savedValue = [[NSUserDefaults standardUserDefaults]
                            stringForKey:@"friendStatus"];
    NSString *accepted = savedValue;

    if (savedValue == NULL){


    } else {



        self.acceptFriend.userInteractionEnabled = NO;
        [self.acceptFriend setImage:[UIImage imageNamed:@"checkedgreen.png"] forState:UIControlStateNormal];


    }
}


- (IBAction)acceptFriend:(id)sender {

    [self alreadyAccepted];
    [self accepted];

        [self.acceptFriend setImage:[UIImage imageNamed:@"checkedgreen.png"] forState:UIControlStateNormal];

}

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    好吧,我可以大致说明如何以一种方式实现这一目标。

    在cell类中声明一个按钮的Outlet

    @property (nonatomic, retain) IBOutlet UIButton *deleteButton;
    

    接下来在cellForRowAtIndexPathdatasource 中将目标分配给您的按钮到ViewController 中的函数。还将tag 分配给此按钮。

    在此函数的操作中,放入用于从模型/数据源中删除数据并重新加载 tableView 的代码和逻辑。

    请参考this linkViewController 中的按钮分配操作。希望这会有所帮助。

    代码片段

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *simpleTableIdentifier = @"SimpleTableItem";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        }
    
        cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
        cell.myButton.tag = indexPath.row
        [cell.myButton addTarget:self 
                 action:@selector(myAction) 
       forControlEvents:UIControlEventTouchUpInside];
            return cell;
        }
    

    【讨论】:

    • 我理解第一部分,但第二部分让我感到困惑:'......将目标分配给视图控制器中的功能 - 也将标签分配给此按钮'。我该怎么做?
    • 如果没有,请尝试更新的答案。我将使用代码 sn-p 更新答案。
    • 请更新代码 sn-p - 大声笑我完全转过身来:/ 哈哈抱歉,谢谢!
    • 2 分钟,我找不到它写下来
    • 嘿!我想我明白了,但我现在的问题是找不到“tableView”(例如,这一行 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; )如果我将它放在我的操作中?我尝试了 self.sidetableView ,但它导致我的应用程序崩溃: -[__NSPlaceholderArray initWithObjects:count:]: 尝试从 objects[0]' 插入 nil 对象
    【解决方案2】:

    我认为一种方法是使用delegate 模式来解决这个问题。

    首先在您的 UITableViewCell 子类中创建一个委托。

    @protocol CellCalssDelegate <NSObject>
    
    - (void)deleteCellForIndexPath:(NSIndexPath *)indexPath;
    
    @end
    
    @interface MyCellSubclass: UITableViewCell
    @property (nonatomic, weak) id <CellCalssDelegate > delegate;
    
    @end
    

    从情节提要的此单元格内的按钮中添加 IBAction。

    -(IBAction) deleteButtonTapped: (UIButton *)button; {
    
                 [self.delegate deleteCellForIndexPath: self.indexPath];
    
    }
    

    然后在您的单元格类中创建indexPath 的属性

    然后在您实现UITableViewDataSourceUITableViewDelegate 方法的类(ViewController) 中。

    ViewContrller.m

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
          ....  
          cell.delegate = self;
    
          cell.indexPath = indexPath;
    
         ...
         return cell;
    }
    
    //CellDelegate Method...
    -(void) deleteCellForIndexPath:(NSIndexPath)indexPath; {
    
            //write your logic for deleting the cell
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      相关资源
      最近更新 更多