我有一个UITableView。每行右侧有一个按钮。现在问题是我点击的任何按钮,它返回indexPath.row的值为0.但是当我点击一个按钮时,它应该返回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];
}
答案 0 :(得分:0)
通过调用:
,您无法获得UITableViewCellUITableViewCell *cell = (UITableViewCell *)[[button superview] superview];
尝试添加一个超级视图来访问该单元格:
UITableViewCell *cell = (UITableViewCell *)[[[button superview] superview] superview];
表格视图上按钮布局的结构是:
UITableViewCellContentView - [sender superview]
UITableViewCellScrollView - [[sender superview] superview]
UITableViewCell - [[[sender superview] superview] superview]
希望这个帮助
答案 1 :(得分:0)
使用关联的对象。这应该有效:
- (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];
}
不要忘记导入runtime.h
#import <objc/runtime.h>
答案 2 :(得分:0)
我的方法是:
我认为做你想做的事情更方便,因为我们想要改变你单元格的视图层次结构。这是一个很好的机会,你的代码需要改变。另外,获得UIKit元素的第三个超级视图永远不是一个好主意。
答案 3 :(得分:-1)
在这种情况下获取索引路径的明确方法是:
NSIndexPath *index = [myTableView indexPathForCell:(UITableViewCell *)[[sender superview]superview]];