基本上,我有自定义单元格布局的UITableView。我已经按照AppCoda.com(http://www.appcoda.com/customize-table-view-cells-for-uitableview/)上的教程了解如何制作它们。
现在,我创建了一个prepareForSegue
方法,当点击UITableView中的单元格时,该方法会打开一个集合视图。因为这样做我的UITableView单元格是空白的,没有我分配给他们的信息。此外,UITableView中的所有单元格都显示相同的数据。
如何在这两者之间建立连接,使其处于完美状态,集合中的第一张图片将用作表格单元格中的样本图像,并且所有表格单元格都有单独的数据?
FolderTableViewController.m
@implementation FoldersTableViewController
{
NSArray *folders;
}
@synthesize tableView;
- (void)viewDidLoad {
[super viewDidLoad];
Folder *folder1 = [Folder new];
folder1.name = @"Barcelona";
folder1.date = @"01/12/2014";
folder1.imageFile = @"city23.gif";
Folder *folder2 = [Folder new];
folder2.name = @"Florence";
folder2.date = @"02/12/2014";
folder2.imageFile = @"city24.gif";
Folder *folder3 = [Folder new];
folder3.name = @"Berlin";
folder3.date = @"03/12/2014";
folder3.imageFile = @"city25.gif";
Folder *folder4 = [Folder new];
folder4.name = @"Swansea";
folder4.date = @"04/12/2014";
folder4.imageFile = @"city26.gif";
Folder *folder5 = [Folder new];
folder5.name = @"Cardiff";
folder5.date = @"05/12/2014";
folder5.imageFile = @"city29.gif";
Folder *folder6 = [Folder new];
folder6.name = @"Pisa";
folder6.date = @"06/12/2014";
folder6.imageFile = @"city30.gif";
Folder *folder7 = [Folder new];
folder7.name = @"Lisbon";
folder7.date = @"07/12/2014";
folder7.imageFile = @"city31.gif";
Folder *folder8 = [Folder new];
folder8.name = @"Liverpool";
folder8.date = @"08/12/2014";
folder8.imageFile = @"city32.gif";
Folder *folder9 = [Folder new];
folder9.name = @"Mannheim";
folder9.date = @"09/12/2014";
folder9.imageFile = @"city33.gif";
folders = [NSArray arrayWithObjects:folder1, folder2, folder3, folder4, folder5, folder6, folder7, folder8, folder9, nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [folders count];
}
// Return the number of rows in the section.
return [folders count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *foldersIdentifier = @"FolderCell";
FolderCellTableViewCell *cell = (FolderCellTableViewCell *) [tableView dequeueReusableCellWithIdentifier:foldersIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FolderCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
Folder *folder = [folders objectAtIndex:indexPath.row];
cell.nameLabel.text = folder.name;
cell.thumbnailImageView.image = [UIImage imageNamed:folder.imageFile];
cell.datumLabel.text = folder.date;
return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showFolderCollection"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];}
}
FolderCollectionViewController.h
@property (strong, nonatomic) NSArray *photoImages;
FolderCollectionViewController.m
@synthesize photoImages;
static NSString * const reuseIdentifier = @"Cell";
- (void)viewDidLoad {
[super viewDidLoad];
self.photoImages = [NSArray arrayWithObjects:@"city23.gif", @"city24.gif", @"city25.gif", @"city26.gif", @"city29.gif", nil];
Folder.h:
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *date;
@property (nonatomic, strong) NSString *imageFile;
Folder.m:
@synthesize name;
@synthesize date;
@synthesize imageFile;
我的自定义表格单元格:
⎮⌈UI⌉姓名⎮
⎮⌊图像⌋日期⎮
这是我的表格单元格的粗略轮廓。我无法发布图片。
对于任何不准确和错误的解释提前道歉,我刚开始学习如何编程,因此我几乎不了解它。
答案 0 :(得分:0)
首先,我怀疑是因为你的TableView不允许选择。因此,[tableView indexPathForSelectedRow]
可能会返回nil
。检查以确保它返回您所期望的。如果它仍然没有,删除UITableViewCell中的segue并从FolderTableViewController创建一个 - > FolderCollectionViewController。然后,在UITableViewDelegate实现:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self performSegueWithIdentifier:@"showFolderCollection" sender:indexPath];
}
其次,在阅读完您的评论之后,我就会如何设置您对segue方法的准备工作:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showFolderCollection"]) {
NSIndexPath *indexPath = [sender isKindOfClass:[NSIndexPath class] ? sender : [self.tableView indexPathForSelectedRow];
Folder *selectedFolder = self.folders[indexPath.row];
FolderCollectionViewController *fcvc = (FolderCollectionViewController *)segue.destinationViewController;
[fcvc loadFolder:selectedFolder];
}
}
请注意,在FolderCollectionViewController
中prepareForSegue
在viewDidLoad
之后和viewWillAppear
之前调用
viewDidLoad
prepareForSegue
viewWillAppear:
所以不要依赖folder
在viewDidLoad
方法中存在FolderCollectionViewController。
- (void)viewDidLoad {
[super viewDidLoad];
// don't load your data here.
}
相反,创建一个这样的方法:
-(void) loadFolder:(Folder*)folder {
self.folder = folder;
self.photoImages = [folder loadAllImages]; // or whatever
[self.collectionView reloadData]; // put this here or in -viewWillAppear:
}
关于文件夹类,我将其定义如下:
@interface Folder : NSObject
+(instanceType) folderWithName:(NSString*)name date:(NSDate*)date andImagePaths:(NSArray*)imagePaths;
@property (nonatomic) NSString *name;
@property (nonatomic) NSDate *date;
@property (nonatomic) NSArray *imagePaths;
@property (nonatomic, readonly) NSString *thumbPath; // use this for the UITableView Image
@end
@implementation Folder
+(instanceType) folderWithName:(NSString*)name date:(NSDate*)date andImagePaths:(NSArray*)imagePaths; {
Folder *folder = [Folder new];
folder.name = name;
folder.date = date;
folder.imagePaths = imagePaths;
}
- (NSString*) thumbPath {
if (self.imagePaths != nil &&
self.imagePaths.count > 0) {
return [self.imagePaths firstObject];
} else {
return nil;
}
}
@end