正确地在自定义表格单元格中获取UIButton的IndexPath.row? [英] Correctly Grab the IndexPath.row of a UIButton in Custom Table Cell?

查看:81
本文介绍了正确地在自定义表格单元格中获取UIButton的IndexPath.row?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表视图,可以从Web加载信息,并将其显示在自定义单元格中。在这些自定义单元格中,我有一个通过故事板分配的按钮。

I have a table view that loads information from the web, and displays it in custom cells. In these custom cells I have a button that I assigned through storyboard.

如果按下此按钮,它会触发一个方法(在cellForRowAtIndexPath方法中定义)

If this button is pressed, it triggers a method (defined in the cellForRowAtIndexPath method)

cell.viewArticleButton.action = @selector(viewArticle:); 

正是在这种方法中我遇到了麻烦。该操作有效,除了它不使用为每个相应单元格提供的链接(每个索引路径上的链接),而是仅使用第一行的链接,无论我点击哪一行。

It is in this method that I am having trouble. The action works except that it doesn't use the link provided for each respective cell (the link at each index path) instead it uses only the first row's link, regardless of what row I tap.

-(IBAction)viewArticle:(id)sender {

NSLog(@"View Article Button Tapped");

NewsCell *cell = (NewsCell *)[self.tableView dequeueReusableCellWithIdentifier:@"NewsCell"];

NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 

MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];

   // Open link from item.link

}

任何帮助将不胜感激。我有一种感觉,就是这条线没有做我想要的:

Any help would be appreciated. I have a feeling that it is this line that isn't doing what I want:

NewsCell *cell = (NewsCell *)[self.tableView dequeueReusableCellWithIdentifier:@"NewsCell"];


推荐答案

为什么不在NSIndexPath中添加add到方法中:

Why not add add in the NSIndexPath into the method:

cell.viewArticleButton.action = @selector(viewArticle:indexPath);

-(IBAction)viewArticle:(id)sender {
NSIndexPath *indexPath = (NSIndexPath *)sender;

NSLog(@"View Article Button Tapped for section %d, row $d", indexPath.section, indexPath.row);

// I have no idea why you are doing this...
NewsCell *cell = (NewsCell *)[self.tableView dequeueReusableCellWithIdentifier:@"NewsCell"];


MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];

   // Open link from item.link

}

这篇关于正确地在自定义表格单元格中获取UIButton的IndexPath.row?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆