调用didSelectRowAtIndexPath时,字符串变为空白

时间:2014-10-16 03:22:32

标签: 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]];
}

当它记录顶部的网址时,一切都很好,但是当它在底部被调用时,没有任何打印。我究竟做错了什么?或者有更好的方法吗?谢谢!

1 个答案:

答案 0 :(得分:3)

请用您给定的代码替换您的didSelectRowAtIndexPath方法......

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    videoURL = [videoURLArray objectAtIndex:indexPath.row];

    NSLog(videoURL);

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:videoURL]];
}