我有一个自定义单元格,它有一个名为 videoURl 的 NSString 属性。我的实现代码是这样的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"RemedyYouTubeTableViewCell";
RemedyYouTubeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RemedyYouTubeTableViewCell" owner:nil options:nil];
for(id obj in topLevelObjects) {
if([obj isKindOfClass:[UITableViewCell class]]) {
cell = obj;
break;
}
}
}
// Configure the cell...
cell.videoThumbnail.image = [UIImage imageNamed:[thumbNailArray objectAtIndex:indexPath.row]];
NSString *stringWithoutPng = [[thumbNailArray objectAtIndex:indexPath.row]
stringByReplacingOccurrencesOfString:@".png" withString:@""];
cell.titleLabel.text = stringWithoutPng;
cell.dateUploadLabel.text = [videoUploadDateArray objectAtIndex:indexPath.row];
cell.durationLabel.text = [videoLengthArray objectAtIndex:indexPath.row];
cell.viewsLabel.text = [videoViewsArray objectAtIndex:indexPath.row];
cell.videoURL = [videoURLArray objectAtIndex:indexPath.row];
NSLog(cell.videoURL);
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
RemedyYouTubeTableViewCell *cell = (RemedyYouTubeTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
NSString *videoURL = cell.videoURL;
NSLog(videoURL);
NSLog(@"^videourl");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:videoURL]];
}
当它在顶部记录 url 时,一切都很好,但是当它在底部被调用时,没有任何打印。我究竟做错了什么?或者有更好的方法吗?谢谢!
最佳答案
请用这个给定的代码替换您的 didSelectRowAtIndexPath 方法...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
videoURL = [videoURLArray objectAtIndex:indexPath.row];
NSLog(videoURL);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:videoURL]];
}
关于ios - 调用 didSelectRowAtIndexPath 时字符串变为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26395707/