CellForRowAtIndexPath不返回相同的didSelectRowAtIndexPath
问题描述:
我有一个UiTableView
包含Youtube视频。 Youtube视频在TableViewCells
中列有属性标题,作者和链接。问题是,当我尝试从选定的indexPath
返回值时,它会返回另一个YouTube视频,而不是所选的一个。为什么它不会从选定的视频中返回信息?CellForRowAtIndexPath不返回相同的didSelectRowAtIndexPath
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);
}
答
- (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);
}
答
你需要写:
video = [items objectAtIndex:indexPath.row];
之前NSLog
。
答
当您编写video = [items objectAtIndex:indexPath.row];
时,您只是存储tableview碰巧请求的最后一项。没有真正的意义在于将其存储为您班级的属性。
您可以使用类似的语句进行didSelectRowAtIndexPath:(NSIndexPath *)indexPath
来检索实际选择视频: Video *video = [items objectAtIndex:indexPath.row];
selectedVideo = [项目objectAtIndex:indexPath.row] –