问题
I've noticed iOS it's very mean when using UICollectionView
inside an UITableViewCell
. What I'm trying to achieve, it's to have a Collection of Images (UICollectionView
approach) inside a UITableViewCell
. I'm trying to mimic Facebook post style, I think, they have an UITableView
, with custom cells and so on, but here's the Problem...
I've managed to put the UICollectionView
inside an UITableViewCell
, and It worked correctly and perfectly, no problem, everything showing fine, it's clickable and so on. BUT, I tried to refactor my code, extracting the UICollectionViewDataSource
& UICollectionViewDelegate
into separate files for Reusing them and having less code on the Main View Controller
, it is here, when my Nightmare begun, I don't know why if I'm using separate files iOS Cycles the configuration of the Cells. For example:
-(void)configureCell:(UICollectionViewCell*)customCell atIndexPath:(NSIndexPath*)indexPath;
The above method it's being called on two parts of the process:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *identifier = [self retrieveIdentifierForIndexPath:indexPath];
UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:identifier];
[self configureCell:myCell atIndexPath:indexPath];
[cell setNeedsLayout];
[cell layoutIfNeeded];
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingExpandedSize].height;
return height;
}
The above method it's invoked to calculate the Row Height using AutoLayout I read this on a Post here at StackOverFlow ( don't remember the post :( )
After the height it's being calculated and so on, another method calls the "configureCell
" method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *rowName = [self retrieveIdentifierForIndexPath:indexPath];
PostDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:rowName forIndexPath:indexPath];
//PreCondition
if([rowName isEqualToString:@"LoadingRow"]){
return cell;
}
// Configure the cell...
[self configureCell:cell atIndexPath:indexPath withImages:YES];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
When the debugger starts, the method configureCell:atIndexPath:
it's being called over and over and over again. this is the second time this happens. Does that mean that I can't "separate" the DataSource
? Or IT IS IMPOSSIBLE to add an UICollectionView
inside an UITableViewCell
. Or, Am I using a bad approach? Is there a way to do this?
I'm very confused on this...
Thank you guys!
回答1:
I struggled with this for a very long time, spent hours redesigning the Layouts, etc, etc. And I finally got the Solution:
This is the Header File of my Cell
@interface MyCustomTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *someLabel;
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
-(void)setImages:(NSArray*)images;
@end
And Here's the Magic beyond all of this the Implementation File (m)
@interface MyCustomTableViewCell() <UICollectionViewDataSource, UICollectionViewDelegate>
@property (strong, nonatomic) NSArray *imageArray;
@end
@implementation MyCustomTableViewCell
-(void)setImages:(NSArray*)images{
_imageArray = images;
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];
[_collectionView reloadData];
}
#pragma mark - UICollectionViewDataSource Methods
...
...
...
#pragma mark - UICollectionViewDelegate Methods
...
...
...
@end
Using the Above approach made the CollectionView show and also show it's content. I also wanted to add an action to the CollectionView, for example, showing MWPhotoBrowser when an Image it's being clicked, I added a method to the Header file of the cell:
-(void)setViewController:(UIViewController*)viewController;
Seted It on the Implementation file and called the MWPhotoBrowser on the Delegate Methods. I know it's really weird, because it forces me, to use a Cell for showing the DataSource & Delegate. When I should be able to recycle my DataSource & Delegate at other points, weird. But It worked!!
Thank you, Guys!
来源:https://stackoverflow.com/questions/25999400/uicollectionview-inside-an-uitableviewcell