iphone - IOS中获取UITableView的行号

标签 iphone ios objective-c uitableview

<分区>

在我的代码中,我有一个 TableView ,其中许多部分按字母顺序排序。我在表行中有 checkboxes(UIButton),我需要根据行中输入的各自状态检查它们。我做过

checkbutton.tag = indexpath.row; 

但是当表格滚动和部分发生变化时,我在我的复选框方法中获得了上一节的状态。任何人都请帮助我。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CheckButton = [UIButton buttonWithType:UIButtonTypeCustom];
   [CheckButton addTarget:self action:@selector(CheckBoxAction:)      forControlEvents:UIControlEventTouchUpInside];
   CheckButton.tag=indexPath.row;
   //some other stuff
   [cell.contentView addSubview:CheckButton];
}

这是我的 CheckBoxAction

-(void)CheckBoxAction:(UIButton *)btn
{
    tag = btn.tag;
    NSDictionary *tagdict =[statusArray objectAtIndex:tag]; 
}

现在,问题是当部分更改时 indexpath.row 为 0,因此我无法在我的 CheckBoxAction 中访问确切的行号。

最佳答案

使用它来获取您点击的当前行。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 CheckButton = [[UIButton alloc] init];

[CheckButton addTarget:self action:@selector(CheckBoxAction:)     forControlEvents:UIControlEventTouchUpInside];
CheckButton.tag=indexPath.row;
[cell addSubview:CheckButton];
}


-(void)CheckBoxAction:(id)sender
{
  // Cast Sender to UIButton
UIButton *button = (UIButton *)sender;

// Find Point in Superview
CGPoint pointInSuperview = [button.superview convertPoint:button.center toView:self.tableView];

// Infer Index Path
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInSuperview];

// Log to Console
NSLog(@"selected Row : %d", indexPath.row);
}

关于iphone - IOS中获取UITableView的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17743546/

相关文章:

ios - 行数在部分

iphone - JSON 到持久数据存储(CoreData 等)

iphone - 如何在 iphone 的 UITableView 中设置单元格的背景颜色?

iPhone 应用程序支持文件夹包含构建前的文件

objective-c - 隐藏和显示 HUD 窗口 - cocoa ?

objective-c - 在不使用私有(private) API 的情况下获取当前的第一响应者

ios - 呈现 Modal 全屏 SwiftUI

ios - 非空数组出现越界错误 - 发生了什么?

ios - 在 iOS6 下使用多个 xibs 进行轮换的问题

ios - nsinternalinconsistencyexception 无法加载 xamarin 包中的 nib