PrepareForSegue始终返回索引0处的单元格

时间:2014-08-10 16:23:49

标签: ios objective-c uitableview xcode5

所以这个问题类似于此处描述的问题UITableView - IndexPath always returns 0 first & takes 2 clicks to pass correct data to view controller但是他们建议的答案无助于解决问题,所以我特意询问我的代码。这是我的prepareForSegue方法:

//send to a detail version of each of the chatters allowing a report a user 
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

if ([[segue identifier] isEqualToString:@"pushToDetail"]){

    ChatterDetailViewController *chatterDetail = [segue destinationViewController];

     UITableViewCell *cell = (UITableViewCell*)sender;

    long indexPathSelected = [self.tableView indexPathForCell:cell].row;

   Chatter *detailChatter = [_arrayWithoutME objectAtIndex:indexPathSelected];

    NSData *fbImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString: detailChatter.url]];

    _detailProfile = [UIImage imageWithData:fbImageData];

   chatterDetail.detailProfile = _detailProfile;
 }
 }

这显然与另一个问题上提出的答案有所不同,但是我尝试了这一点并没有改变任何东西。每当我点击tableView单元格时,它就会完成segue,我会看到第一个单元格(索引0)中的轮廓图片,而不是单击它的单元格。我尝试使用

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

然而它并没有像我需要的那样传递图像值,而且当我尝试使用展开segue返回时,它表示导航树已损坏(当前代码不会发生)。基本上我似乎无法找到为什么选择任何单元格只返回第一个单元格的信息。

编辑:

这是来自Chatter Detail的一些代码的视图加载方法:

- (void)viewDidLoad
{
   [super viewDidLoad];
   self.navigationItem.hidesBackButton = YES;
   _detailProfilePicture.contentMode = UIViewContentModeScaleAspectFill;
   _detailProfilePicture.image = _detailProfile;
   [_detailProfilePicture sizeToFit];

 }

同样,它确实会返回一张图片,但它只是索引为0的单元格中的图片。

1 个答案:

答案 0 :(得分:1)

有一种更简单的方法可以在UITableView中为所选行提取indexPath:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"pushToDetail"]) {

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

    // section and row return an int like this:
    NSLog(@"index path: section %i, row %i", indexPath.section, indexPath.row);

}
}

但是,您的代码确实提供了正确的行值,并且确实可以使用long来从NSArray获取对象。我会在此方法的末尾添加断点或日志消息,并查看数组中是否存在正确的值。

我还会检查你在indexPathSelected中获得的值 - 这应该反映所选的行。