2 exit door eXit_door 于 2013.05.03 11:23 提问

将电影标题传递到视图中

我能解析Json文件到表视图中,但是不知道怎么获取电影标题,作为标题传递到详细视图中。

我用这样的方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"movieCell";
    movieCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    id video = [self.model.videos objectAtIndex:indexPath.row];
    NSString *rl = [video objectForKey:@"release_date"];
    NSString *imageURL;
    if ([[video objectForKey:@"poster_path"] class] != [NSNull class])
        imageURL = [video objectForKey:@"poster_path"];
    else
        imageURL = @"icon.jpg";

    if (rl != (NSString *)[NSNull null]){
            NSString *dateStr = rl;
            // Convert string to date object
            NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"yyyy-MM-dd"];
            NSDate *date = [dateFormat dateFromString:dateStr];

            // Convert date object to desired output format
            [dateFormat setDateFormat:@"MM/YYYY"];
            NSString *released = @"Released: ";
            NSString *fullDate;
            dateStr = [dateFormat stringFromDate:date];
            fullDate = [NSString stringWithFormat:@"%@ %@", released, dateStr];
            cell.videoDescriptionLabel.text = fullDate;
    }
    else{
        cell.videoDescriptionLabel.text = @"N/A";
    }
    cell.videoTitleLabel.text = [video objectForKey:@"title"];      
    NSString* thumbnailURL = [NSString stringWithFormat:@"http://cf2.imgobject.com/t/p/w154%@",imageURL];
    [cell.videoThumbnail setImageWithURL:[NSURL URLWithString:thumbnailURL] placeholderImage:[UIImage imageNamed:@"icon"]];
    return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    DetailViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"boot"];
    [self.navigationController pushViewController:vc animated:YES];
}

2个回答

xinyoulingxi1_tong
xinyoulingxi1_tong   2013.05.03 15:00
已采纳
id video = [self.model.videos objectAtIndex:indexPath.row];
vc.title = [video objectForKey:@"title"];

另外,你不应使用id作为video的类型。

eXit_door
eXit_door 第一次没成功,后来好了,谢谢
大约 5 年之前 回复
lianlianbushell
lianlianbushell   2013.05.03 13:54

假设有一个命名videoTitle的属性,放在 DetailViewController中:

@property (nonatomic, strong) NSString *videoTitle;

就可以这样:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    DetailViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"boot"];
    vc.videoTitle = [[self.model.videos objectAtIndex:indexPath.row] objectForKey:@"title"];
    [self.navigationController pushViewController:vc animated:YES];
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!