美文网首页
在自定义的方法获取点击的cell的行号

在自定义的方法获取点击的cell的行号

作者: Tanyfi | 来源:发表于2016-09-22 15:06 被阅读81次

    在自定义的方法获取点击的cell的行号

    用到的方法:

    - (nullable NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;                      // returns nil if cell is not visible
    - (nullable NSArray<NSIndexPath *> *)indexPathsForRowsInRect:(CGRect)rect;                              // returns nil if rect not valid
    
    - (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
    @property (nonatomic, readonly, nullable) NSIndexPath *indexPathForSelectedRow; // returns nil or index path representing section and row of selection.
    @property (nonatomic, readonly, nullable) NSArray<NSIndexPath *> *indexPathsForSelectedRows NS_AVAILABLE_IOS(5_0); // returns nil or a set of index paths representing the sections and rows of the selection.
    
    // Selects and deselects rows. These methods will not call the delegate methods (-tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath:), nor will it send out a notification.
    - (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
    - (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
    

    结合通知观察者 把cell的信息发送到控制器, 在控制器的方法中获取具体点击的某个cell的位置

    • 在cell中 点击按钮的方法中:

      - (void)delectAction{
         [[NSNotificationCenter defaultCenter] postNotificationName:@"deleteAction" object:self];}
      
    • 在控制器的viewDidLoad()添加观察者

      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteAction:) name:@"deleteAction" object:nil];
    
    • 实现具体的方法
      - (void)deleteAction:(NSNotification *)note{
        StoreShopsListCell *cell = note.object;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        NSLog(@"我只是调试用的~哈哈哈😄---%zd",indexPath.row);
        if (self.shopingCarArr.count >0) {
            [self.shopingCarArr removeObjectAtIndex:indexPath.row];
        }
    }
    

    相关文章

      网友评论

          本文标题:在自定义的方法获取点击的cell的行号

          本文链接:https://www.haomeiwen.com/subject/cjuzettx.html