gpt4 book ai didi

ios - 没有获取用于在 UITableViewCell 中点击 UISwitch 的 indexPath

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:23:53 26 4
gpt4 key购买 nike

我在 UITableViewCell 中添加了一个 UISwitch,table 内容是动态的,这意味着一个 tableview 中可能有很多 UISwitch,我需要为每个 UITableViewCell 获取 UISwitch 状态,但没有获取 indexPath accessoryButtonTappedForRowWithIndexPath 方法中的。

我的代码是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
LocationCell *cell = (LocationCell *)[tableView
dequeueReusableCellWithIdentifier:@"LocationCell"];

UISwitch *useLocationSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[cell addSubview:useLocationSwitch];
cell.accessoryView = useLocationSwitch;

[useLocationSwitch addTarget: self
action: @selector(accessoryButtonTapped:withEvent:)
forControlEvents: UIControlEventTouchUpInside];

return cell;
}

- (void) accessoryButtonTapped: (UIControl *) button withEvent: (UIEvent *) event
{
NSIndexPath * indexPath = [showLocationTableView indexPathForRowAtPoint: [[[event touchesForView: button] anyObject] locationInView: showLocationTableView]];
if ( indexPath == nil )
return;

[showLocationTableView.delegate tableView: showLocationTableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
NSLog(@"index path: %@", indexPath.row);
}

最佳答案

控件事件应该是UIControlEventValueChanged

不是 UIControlEventTouchUpInside。更改它并重试。

所以 Action 设置语句应该如下:

[useLocationSwitch addTarget: self
action: @selector(accessoryButtonTapped:withEvent:)
forControlEvents: UIControlEventValueChanged];

编辑:

- (void) accessoryButtonTapped: (UIControl *) button withEvent: (UIEvent *) event
{
UISwitch *switch1 = (UISwitch *)button;
UITableViewCell *cell = (UITableViewCell *)switch1.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

//NSIndexPath * indexPath = [showLocationTableView indexPathForRowAtPoint: [[[event touchesForView: button] anyObject] locationInView: showLocationTableView]];
if ( indexPath == nil )
return;

[showLocationTableView.delegate tableView: showLocationTableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}

关于ios - 没有获取用于在 UITableViewCell 中点击 UISwitch 的 indexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10009515/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com