【发布时间】:2014-10-17 08:07:52
【问题描述】:
我使用 nib 文件创建了一个自定义 UITableViewCell。然后当我选择我的自定义 UITableViewCell 时,我想执行 segue 并将数据传递给详细视图控制器。我已经在storyboard中设置了segue标识符,我把prepareForSegueWithIdentifier放在了- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath中,但是当我执行segue的时候,详细页面上只能显示表格视图的第一行。
我有三个问题:
1.如何为自定义 UITableViewCell 设置 segue 标识符?
由于我在 xib 文件中创建了自定义 UITableViewCell,因此很难将 xib 文件与主情节提要与其他视图控制器连接起来。
2。如果我在故事板中更改表格视图单元格的类名,它会是我的自定义 UITableViewCell 吗?
具体来说,为了将storyboard中默认的UItableViewCell替换为我的自定义UITableViewCell,我将storyboard中默认UITableViewCell的类名改为我自定义的UITableViewCell。然后我 Control + Drag 创建一个 push segue 并设置 segue 标识符。不过这个好像也行不通。
3.为什么每次我执行segue,目标视图控制器只显示我的表格视图第一行的具体内容?
也就是说,每当我选择第一个单元格、第二个单元格、第三个单元格......时,我在 segue 到目标视图控制器后得到的只是第一个单元格的内容。我想知道发件人是否是我的自定义 UITableViewCell?还是 indexPath 为零?我不确定。
我的代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
customTableViewCell *cell;
cell = (customTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if (!cell) {
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"customTableViewCell" owner:nil options:nil];
cell = [nib objectAtIndex:0];
}
NSDictionary *photo = self.recentPhotos [indexPath.row];
dispatch_queue_t queue = dispatch_queue_create("fetch photos", 0);
dispatch_async(queue, ^{
NSURL *imageURL = [FlickrFetcher URLforPhoto:photo format:FlickrPhotoFormatSquare];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
dispatch_async(dispatch_get_main_queue(), ^{
NSString *title = [photo valueForKeyPath:FLICKR_PHOTO_TITLE];
cell.cellLabel.text = title;
UIImageView *cellImageView = [[UIImageView alloc]initWithImage:image];
[cellImageView setFrame: CGRectMake(10, 5, 45, 45)];
[cellImageView.layer setCornerRadius:cellImageView.frame.size.width/2];
cellImageView.clipsToBounds = YES;
[cell addSubview:cellImageView];
});
});
return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"go" sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
NSDictionary *photo = self.recentPhotos [indexPath.row];
NSURL *imageURL = [FlickrFetcher URLforPhoto:photo format:FlickrPhotoFormatLarge];
NSString *title = [photo valueForKeyPath:FLICKR_PHOTO_TITLE];
if ([segue.identifier isEqualToString:@"go"]) {
if ([segue.destinationViewController isKindOfClass:[ViewImageViewController class]]) {
ViewImageViewController *vivc = (ViewImageViewController *) segue.destinationViewController;
vivc.imageURL = imageURL;
vivc.title = title;
}
}
}
【问题讨论】:
标签: ios objective-c iphone uitableview