【问题标题】:UITableView selected indexpath value gives wrongUITableView 选择的 indexpath 值给出错误
【发布时间】:2017-05-04 05:12:39
【问题描述】:

团队,

我有 UITableView,其中有 50 多个单元格。 每个单元格的宽度为 60。

当我最多滚动 20 个单元格时,然后点击任何单元格

它给出了高于单元格值的索引路径值而不是单击单元格值

CellForRowAtIndexPath 内部

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    [[cell checkButton] addTarget:self action:@selector(checkButtonAction:) forControlEvents:UIControlEventTouchUpInside];

}

   -(void)checkButtonAction:(id)sender

    CGPoint touchPoint = [sender convertPoint:CGPointZero toView:self.tableview];

    NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:touchPoint];

    UIButton *button = (UIButton *)sender;

 }

如果选择的索引为 21,则索引为 20,保持索引计数从零开始。

在 iOS 10.1 iphone 7 plus 设备中观察到该问题,而不是在模拟器 iPhone 7 plus iOS 10.1 (14B72) 中观察到

调试值 {长度 = 2,路径 = 0 - 14}

NSIndexPath 路径应该是 0 - 15,但它给出的是 0 - 14。

【问题讨论】:

  • 尝试在转换点用 ((UIButton*)sender) 替换发件人;
  • 如果还是同样的问题,那么用下面的“[sender convertPoint:CGPointZero toView:self.view]”试一次
  • @kiran checkButtonAction: 是你按钮的触摸事件吗?
  • 转换点?我很震惊。
  • @sanket 还是一样的问题。

标签: ios objective-c uitableview didselectrowatindexpath nsindexpath


【解决方案1】:
[YOURBUTTON addTarget:self action:@selector(METHODNAME:event:) forControlEvents:UIControlEventTouchDown];

按钮点击方式

-(IBAction)METHODNAME:(UIButton*)sender event:(id)event
{

    NSSet *touches = [event allTouches];

    UITouch *touch = [touches anyObject];

    CGPoint currentTouchPosition = [touch locationInView:YOUR_TABLEVIEW];

    NSIndexPath *indexPath = [YOUR_TABLEVIEW indexPathForItemAtPoint: currentTouchPosition];
   // your requirement code 
}

如果对此代码有任何疑问,请将您的评论放在 answer 中。 祝你好运

快乐编码。

【讨论】:

  • NSSet *touches = [sender allTouches]; UITouch *touch = [触摸任何对象]; CGPoint currentTouchPosition = [触摸 locationInView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];它崩溃不工作。
  • @kiran 你有什么问题?您只需在代码中注释我的代码即可。
  • [UIButton allTouches]:无法识别的选择器发送到实例这是我的崩溃日志
  • @kiran 我在我的项目中使用上面的代码并且它工作正常。现在只需用我的代码实现更新您的问题。并更新错误
  • 问题只在iphone 6s设备上。我还需要知道您正在测试哪种设备。该问题在模拟器中不出现,仅在 iPhone 6s 设备中可见。
【解决方案2】:

使用下面的代码在UIButton点击操作方法中获取NSIndexPath,它工作正常。

试试下面的代码并检查:

-(void)checkButtonAction:(id)sender

      UIButton *btn = (UIButton *)sender;

      CGPoint origin = btn.frame.origin;
      CGPoint touchPoint = [btn.superview convertPoint:origin toView:self.tableview];

      NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:touchPoint];

}

【讨论】:

  • 我改了还是一样的问题。
【解决方案3】:

首先尝试这个,在 cellForRowAtIndexPath 方法中给按钮提供标签号

yourButton.tag = indexPath.row;


[YOURBUTTON addTarget:self action:@selector(checkButtonAction:) forControlEvents:UIControlEventTouchUpInside]

和这样的获取

-(void)checkButtonAction:(id)sender

     UIButton * button = (UIButton *)sender;
     int indexPathOfCell = sender.tag;
}

【讨论】:

  • 还添加 [YOURBUTTON addTarget:self action:@selector(checkButtonAction:) forControlEvents:UIControlEventTouchUpInside];在 cellForRowAtIndexPath 内
  • 仅供参考,在这种情况下,如果表有多个部分,我们将无法获得正确的 indexPath。
  • @Mahesh - 是的,完全同意。但是,如果提问者只有一个部分,那么这将是目前的最佳解决方案。
  • @Sanket 我不知道为什么,但提问者没有回应任何人的反馈和询问。
【解决方案4】:

正如 mahesh 指出的 Gurinder Batth 解决方案的一个问题,它无法获取 indexPath 的部分信息。所以我想补充一个答案。

创建继承自 UITableViewCell 的新 Objective-C 类。让我们说“MyTableViewCell”

现在通过创建 UIButton 的 IBOutlet 来修改 MyTableViewCell.h(通过控制 + 单击 + 从 UIButton 拖动到 MyTableViewCell.h)

#import <UIKit/UIKit.h>

@class MyTableViewCell;
@protocol MyTableViewCellDelegate <NSObject>
- (void)checkButtonClicked:(MyTableViewCell *)cell;
@end

@interface MyTableViewCell : UITableViewCell

@property (weak, nonatomic) id <MyTableViewCellDelegate> delegate;
@property (weak, nonatomic) IBOutlet UIButton *checkButton;

@end

然后通过创建UIButton的IBAction来修改MyTableViewCell.m(通过控制+点击+从UIButton拖动到MyTableViewCell.m)

@implementation MyTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (IBAction)checkButtonAction:(id)sender {
    [self.delegate checkButtonAction:self];
}

@end

现在打开你的 ViewController.m 文件并实现我们在 MyTableViewCell.h 中创建的协议

@interface ViewController () <UITableViewDataSource, UITableViewDelegate, MyTableViewCellDelegate>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view layoutIfNeeded];

    [self.myTableView setDataSource:self];
    [self.myTableView setDelegate:self];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableViewCell *myTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"MyTableViewCell" forIndexPath:indexPath];

    [myTableViewCell setDelegate:self];

    return myTableViewCell;
}

#pragma mark - MyTableViewCellDelegate method

- (void)checkButtonClicked:(MyTableViewCell *)cell {
    NSIndexPath *indexPath = (NSIndexPath*)[self.myTableView indexPathForCell:cell];
    // Here access row using "indexPath.row"
    // You can access section using "indexPath.section"
}

@end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多