ios - 调用 didSelectRowAtIndexPath 时字符串变为空白

标签 ios objective-c didselectrowatindexpath

我有一个自定义单元格,它有一个名为 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/

相关文章:

ios - 设置 UIScrollView 的 contentInset 为 KeyboardWillShowNotification 无法正常工作

ios - iOS NSTimer触发两次

ios - 如何防止更新UITableView中的单元格?

objective-c - 使 Cocoa 窗口全屏显示

ios - 当我单击 UITableViewCell 中的按钮时,UITableView 不会调用 didSelectRowAtIndexPath 函数

ios - 根据行选择转至 UITableView

objective-c - 在 didSelectRowAtIndexPath 中记录特定行

ios - 有什么方法可以在最新版本的 iOS 上使用不同团队 ID 签名的白名单应用程序之间安全地共享凭证?

iphone - 如何将文本字段中的文本插入到警报 View 中

objective-c - 关于未出现的缺少@required 协议(protocol)方法的编译警告