CellForRowAtIndexPath不会返回与didSelectRowAtIndexPath相同的内容

时间:2014-01-15 11:00:43

标签: ios objective-c uitableview

我有一个包含Youtube视频的UiTableView。 Youtube视频列在TableViewCells中,其中包含属性标题,作者和链接。问题是,当我尝试从选定的indexPath返回值时,它会返回另一个YouTube视频而不是所选的视频。为什么不从所选视频中返回信息?

的cellForRowAtIndexPath;

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

{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];



    video = [items objectAtIndex:indexPath.row];

    cell.textLabel.text = video.title;
    cell.textLabel.numberOfLines = 2;
    UIFont *myFont = [ UIFont fontWithName: @"Arial" size: 12.0 ];
    cell.textLabel.font  = myFont;

    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@\n%@ views", video.author, FormatViewCount(video.viewcnt)];
    cell.detailTextLabel.numberOfLines = 2;
    UIFont *myFont1 = [ UIFont fontWithName: @"Arial" size: 10.0 ];
    cell.detailTextLabel.font  = myFont1;

    [cell.imageView setImageWithURL:[NSURL URLWithString:video.thumb] placeholderImage:[UIImage imageNamed:@"blankvideo"]];
    [cell.imageView.layer setBorderWidth:2];
    [cell.imageView.layer setBorderColor:[UIColor whiteColor].CGColor];

    return cell;
}

didSelectRowAtIndexPath方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        NSLog(@"%@", video.title);
        NSLog(@"%@", video.author);
        NSLog(@"%@", video.videoid);
        NSLog(@"%d", thisObject.selectedRowNow);


}

3 个答案:

答案 0 :(得分:2)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
       //---------add one more line---------//
        video = [items objectAtIndex:indexPath.row];
       //---------add one more line---------//
        NSLog(@"%@", video.title);
        NSLog(@"%@", video.author);
        NSLog(@"%@", video.videoid);
        NSLog(@"%d", thisObject.selectedRowNow);


}

答案 1 :(得分:1)

你需要写:

video = [items objectAtIndex:indexPath.row];
NSLog之前

答案 2 :(得分:0)

当您编写video = [items objectAtIndex:indexPath.row];时,您只是存储了tableview恰好请求的最后一项。没有必要将它存储为你班级的属性。

您可以在didSelectRowAtIndexPath:(NSIndexPath *)indexPath中使用类似的声明来检索实际选择的视频: Video *video = [items objectAtIndex:indexPath.row];