uitaiteView indexpath.按按钮操作的行为错误



我有一个UITAITHVIEW。每行右侧都有一个按钮。现在问题是我点击的任何按钮,它将返回我的IndexPath.row的值。但是,当我点击一个按钮时,它应该返回我的IndexPath.row的相应值。我在iOS 7中工作。我在哪里做的错误?

这是用于在单元格上创建按钮的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger index = [indexPath row];
UISegmentedControl  *renameButton = nil;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    renameButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Rename"]];
    [renameButton setTag:6];
    [renameButton addTarget:self action:@selector(renameButtonClicked:) forControlEvents:UIControlEventValueChanged];
    [renameButton setSegmentedControlStyle:UISegmentedControlStyleBar];
    [renameButton setMomentary:YES];
    [renameButton setTintColor:[UIColor lightGrayColor]];
    [cell.contentView addSubview:renameButton];
    [renameButton release];
return cell;

这是选择器方法的代码:

- (void)renameButtonClicked:(id)sender {
isInEditMode = !isInEditMode;
[self setToolbarUserInterface];
[self setTableViewUserInterface];
while ([selectedItems count] > 0) {
    [selectedItems removeLastObject];
}
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];
int index = [[self.tableView indexPathForCell:cell] row];
NSLog(@"Index value : %d",index);
NSString *newPath = [path stringByAppendingPathComponent:[items objectAtIndex:index]];
TextInputViewController *textInput = [[TextInputViewController alloc] init];
[textInput setPath:newPath Parent:parent andType:5];
[self.navigationController pushViewController:textInput animated:YES];
}

您不会通过致电:

获得UITATION ViewCell
UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];

尝试再添加一个Supperiew访问该单元格:

UITableViewCell *cell = (UITableViewCell *)[[[button superview] superview] superview];

表视图上的按钮布局是:

UITableViewCellContentView - [sender superview]
UITableViewCellScrollView - [[sender superview] superview]
UITableViewCell - [[[sender superview] superview] superview]

希望此帮助

使用关联的对象。这应该有效:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger index = [indexPath row];
UISegmentedControl  *renameButton = nil;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    renameButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Rename"]];
    [renameButton setTag:6];
    [renameButton addTarget:self action:@selector(renameButtonClicked:) forControlEvents:UIControlEventValueChanged];
    [renameButton setSegmentedControlStyle:UISegmentedControlStyleBar];
    [renameButton setMomentary:YES];
    [renameButton setTintColor:[UIColor lightGrayColor]];
    [cell.contentView addSubview:renameButton];
    objc_setAssociatedObject(renameButton, "cell", cell, OBJC_ASSOCIATION_ASSIGN);
    [renameButton release];
return cell;

选择器方法:

- (void)renameButtonClicked:(id)sender {
isInEditMode = !isInEditMode;
[self setToolbarUserInterface];
[self setTableViewUserInterface];
while ([selectedItems count] > 0) {
    [selectedItems removeLastObject];
}
UITableViewCell *cell = objc_getAssociatedObject(sender, "cell");
int index = [[self.tableView indexPathForCell:cell] row];
NSLog(@"Index value : %d",index);
NSString *newPath = [path stringByAppendingPathComponent:[items objectAtIndex:index]];
TextInputViewController *textInput = [[TextInputViewController alloc] init];
[textInput setPath:newPath Parent:parent andType:5];
[self.navigationController pushViewController:textInput animated:YES];
}

不要忘记导入运行时。h

#import <objc/runtime.h>

我的方法就是这样:

  1. 创建一个自定义按钮类,该类别根据您正在使用的UI元素继承一个类别的类别,在您的情况下,它是 uisegrentedControl
  2. 然后添加一个属性以保存其索引路径。
  3. 当您创建 uisegentedControl 设置其索引路径时。
  4. 在您的动作方法中,您可以简单地获得其索引路径。

我认为这是做您想做的事情更方便的方法,因为可以说,您想更改单元格的视图层次结构。需要更改代码的好机会。另外,获得UIKIT元素的第三个监管绝不是一个好主意。

在这种情况下,获得indexpath的清晰方法是:

NSIndexPath *index = [myTableView indexPathForCell:(UITableViewCell *)[[sender superview]superview]];

最新更新