objective-c - 单击按钮时如何传递对象

标签 objective-c ios xcode

我有一个带有按钮的自定义表格 View 单元格。单击按钮时,我需要传递 tablview 的 NSIndexPath 对象。我能做的是将标签分配给按钮,使用发件人接收标签..但我想传递 NSIndexPath 对象...下面是代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
   static NSString *CellIdentifier = @"shortCodeCell";
   IPadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil)
   {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"KeywordsCell"     owner:nil options:nil];

        for (id currentObject in topLevelObjects)
        {   
            if ([currentObject isKindOfClass:[IPadTableViewCell class]])
            {
                cell = (IPadTableViewCell  *)currentObject;
            }
        }
    }
    // Delete
    [cell.btnDelete addTarget:self action:@selector(onDelete:) forControlEvents:UIControlEventTouchUpInside];

    cell.btnDelete.tag=indexPath.row;

    return cell;
}

-(void)onDelete:(id)sender
{   
     UIButton *btn=(UIButton *)sender;
     NSLog(@"BtnIndexpath to be deleted:%d",btn.tag);
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
     int errorCode = 0;
     kd = [items objectAtIndex:btn.tag];
     BOOL isDeleteKeyword= [ServerAPI deleteKeywordWithId:kd.keywordId :&errorCode];
     dispatch_async (dispatch_get_main_queue (),
                    ^{
                        if (isDeleteKeyword) {
                            [items removeObjectAtIndex:btn.tag];
                            //[tblKeywords deleteRowsAtIndexPaths:[NSArray arrayWithObject:btnIndexPath] withRowAnimation:YES];
                            [self.tblKeywords reloadData];
                        }
                        else return;
                    });
    });
}

最佳答案

您可以在“cellForRowAtIndexPath”中设置以下内容

cell.tag = indexPath.section; 
cell.btnDelete.tag=indexPath.row;

在“onDelete”中,您可以根据分配的标签创建 indexPath。

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:[[sender superview] tag]]

关于objective-c - 单击按钮时如何传递对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13538696/

相关文章:

iphone - 在 obj-c 中使用正则表达式在字符串中搜索模式

ios - 创建重复事件而不在 iOS 的日历中显示这些事件

ios - 创建具有最大大小并在添加更多项目时删除项目的 NSMutableArray 的最佳途径是什么?

ios - 如何抛出SKSpriteNode?

javascript - react-native app 已经启动,现在添加导航

iOS map 显示特定城市的中心

objective-c - 主屏幕上缺少 WatchKit 应用程序图标

iOS:将 CIFilter 应用于 UIImage 时,结果图像会旋转

ios - 自定义 View 单元格的属性始终设置为 nil (UITableView)

ios - 我为 Real iOS 设备设置 Appium,无法启动 WebDriverAgent,因为 xcodebuild 失败,代码为 65