我正在将核心数据中的对象保存到下面列出的单元格中。 URL正在正确保存到单元格并且运行良好。我的问题是,当用户点击一个单元格时,我希望保存到该单元格的URL传递给我的detailedViewController
以供使用。我有一些我尝试过的代码,但是detailedViewController
中的url为nil。如果你有更好的方法来完成同样的事情,那就没问题了。代码列在下面 -
这是我将其保存到单元格的位置:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
PhotoCell *cell = (PhotoCell *)[self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[PhotoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
// Configure the cell...
FeedEntity *feed = [_fetchedResultsController objectAtIndexPath:indexPath];
NSData *data = feed.imageData;
self.feedImage = [UIImage imageWithData:data];
cell.thumbImage = self.feedImage;
NSData *stringData = feed.urlString;
self.stringForURL = [[NSString alloc] initWithData:stringData encoding:NSUTF8StringEncoding];
self.stringForURL = [self.stringForURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
self.finalURL = [NSURL URLWithString:self.stringForURL];
cell.finalURL = self.finalURL;
return cell;
}
这是我从单元格中检索url并将其传递给detailedViewController的地方:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
// Code to create detailed view and set properties
self.detailsViewController = [[DetailsViewController alloc] init];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
FeedEntity *feed = [_fetchedResultsController objectAtIndexPath:path];
NSData *stringData = feed.urlString;
NSString *stringURL = [[NSString alloc] initWithData:stringData encoding:NSUTF8StringEncoding];
NSLog(@"Here is the string before: %@", stringURL);
stringURL = [self.stringForURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *urlForDetail = [NSURL URLWithString:self.stringForURL];
NSLog(@"Here is the url before it goes to the detailed: %@", urlForDetail);
self.detailsViewController.finalURL = urlForDetail;
[self.navigationController pushViewController:self.detailsViewController animated:YES];
}
保存视频(在didFinishPickingMediaWithInfo中:):
self.videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfURL:self.videoURL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingFormat:@"/vid1.mp4"];
self.urlForSave = [NSURL fileURLWithPath:path];
//Look at YES
[videoData writeToFile:path atomically:YES];
[self saveImageAndVideo];
这是SaveVideoAndPhoto:
- (void)saveImageAndVideo {
NSManagedObjectContext *context = [self managedObjectContext];
FeedEntity *feedEntity = [NSEntityDescription insertNewObjectForEntityForName:@"FeedEntity" inManagedObjectContext:context];
NSData *imageData = UIImageJPEGRepresentation(self.thumbImage, 0.8f);
self.photoData = imageData;
NSString *stringForSave = [self.urlForSave absoluteString];
NSLog(@"URL before save: %@", stringForSave);
//NSData * stringData = [stringForSave dataUsingEncoding:NSUTF8StringEncoding];
[feedEntity setValue:imageData forKey:@"imageData"];
[feedEntity setValue:[NSDate date] forKey:@"timeStamp"];
[feedEntity setValue: stringForSave forKey:@"urlString"];
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
} else {
NSLog(@"URL's are being saved");
}
}
答案 0 :(得分:0)
您的问题出在cellForRowAtIndexPath中的代码中:
self.finalURL = [NSURL URLWithString:self.stringForURL];
您将URL设置为SELF的属性,在本例中是您的viewController。你想在CELL上设置它。从self.whatever cell创建单元格时更改所有代码。如果要将它们保存为单元格的属性,则更改。如果您在objective-c中读取范围,它可能对您有所帮助。
另外,在旁注中,您正在做的一些事情是不必要的。首先是:
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
您不需要在此函数中创建indexPath对象,因为函数本身已经为变量indexPath提供了它:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
其次,在didSelectRowAtIndexPath内部,如果你想获取网址,你应该这样做:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// deselect the row if you want the cell to fade out automatically after tapping
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// get a reference to the cell that the user tapped
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// get the url from the cell, assuming your cell has a property called finalURL on it that you set some value
// to originally when it was created
NSURL *url = cell.finalURL;
// do something with that URL here...
}
请记住,这有点不正统。你真的应该从tableView的数据源中获取信息。如果你有一个用来填充tableView的对象数组,那么简单地使用给定的indexPath从我们的数组中获取对象本身可能是一个更好的主意,而不是将信息作为属性保存在单元格中并以这种方式访问它。我强烈建议观看一些教程视频或进行一些阅读,最好是在iOS文档中,尝试学习UITableViews的最佳实践。