Adding UICollectionViewLayout programmatically issue - ios

I'm trying to implements this library without using storyBoard (first step for implementing this library) , because I'm creating my UIcollectionView programmatically.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.collectionView];
[_collectionView registerNib:[UINib nibWithNibName:#"myCell" bundle:nil] forCellWithReuseIdentifier:#"cell3"];
[_collectionView setBackgroundColor:[UIColor colorWithRed:0.945 green:0.945 blue:0.945 alpha:1] ];
[_collectionView setTransform:CGAffineTransformMakeScale(-1, 1)];
RFQuiltLayout* layout = (id)[_collectionView collectionViewLayout];
layout.direction = UICollectionViewScrollDirectionVertical;
layout.blockPixels = CGSizeMake(100, 100);
}
- (UICollectionView *)collectionView {
if (!_collectionView) {
CGRect collectionViewFrame = self.view.bounds;
collectionViewFrame.size.height -= (self.navigationController.viewControllers.count > 1 ? 0 : (CGRectGetHeight(self.tabBarController.tabBar.bounds))) + 0;
// FMMosaicLayout *mosaicLayout = [[FMMosaicLayout alloc] init];
//// _collectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:mosaicLayout];
// RFQuiltLayout* layout = (id)[_collectionView collectionViewLayout];
// layout.direction = UICollectionViewScrollDirectionVertical;
// layout.blockPixels = CGSizeMake(100, 100);
_collectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:[[RFQuiltLayout alloc] init]];
_collectionView.delegate = self;
_collectionView.dataSource = self;
}
return _collectionView;
}
But this didn't worked and nothing is shown in my view (no error and empty view that's all) Also using debugger I've notified that the UICollectionView Method are never called

First of all, your collectionView method is not called because you are using _collectionView instead self.collectionView in your viewDidLoad method. You need to write self for every property to call their setter and getter methods.
Second, if you want to add custom layout without Storyboard of XIB, then you need to set it programmatically:
RFQuiltLayout* layout = [[RFQuiltLayout alloc] init];
layout.direction = UICollectionViewScrollDirectionVertical;
layout.blockPixels = CGSizeMake(100, 100);
self.collectionView.collectionViewLayout = layout;

Related

how to suspend reloadData of tableview being called?

reloadData is NSLog out after viewDidLayoutSubviews,
I want the tableview call reloadData after GetDataSource , no reloadData on first loading. I even try set _tableView.dataSource = nil; and set it to self in GetDatasource();
But reloadData keeps being called before GetDatasource();
GetDatasource(); is in viewDidload();
viewWillAppar: <ATOMCutstomNavigationController: 0x1476abfc0>
viewDidload
viewWillAppear
viewWillAppear: <ATOMMyFollowViewController: 0x14751b4c0>
viewWillLayoutSubviews
viewDidLayoutSubviews
reloadData
viewWillLayoutSubviews
viewDidLayoutSubviews
viewDidAppear
GetDataSource()
Anyone has any hint?
How can I suspend reloadData or what mistake I have made ?
code In viewDidload:
_myAttentionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAV_HEIGHT - TAB_HEIGHT)];
self.view = _myAttentionView;
_tableView = [[RefreshTableView alloc] initWithFrame:_myAttentionView.bounds];
[_tableView registerClass:[kfcFollowCell class] forCellReuseIdentifier:CellIdentifier];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.estimatedRowHeight = SCREEN_HEIGHT - NAV_HEIGHT - TAB_HEIGHT;
_tableView.delegate = self;
_tableView.psDelegate = self;
_tableView.dataSource = nil;
[_myAttentionView addSubview:_tableView];
_tapMyAttentionGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapMyAttentionGesture:)];
[_tableView addGestureRecognizer:_tapMyAttentionGesture];
_canRefreshFooter = YES;
_dataSource = [NSMutableArray array];
[self firstGetDataSource];
- (void)getDataSource {
Model *sss = [ATOMShowAttention new];
[sss Get:param withBlock:^(NSMutableArray *resultArray, NSError *error) {
if (resultArray.count) {
[_dataSource removeAllObjects];
}
for (ATOMCommonImage *commonImage in resultArray) {
kfcFollowVM * viewModel = [kfcFollowVM new];
[viewModel setViewModelData:commonImage];
[_dataSource addObject:viewModel];
}
if (!_hasSetSource) {
_tableView.dataSource = self;
_hasSetSource = YES;
}
[_tableView reloadData];
[_tableView.header endRefreshing];
}];
}
in view did load
_myAttentionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAV_HEIGHT - TAB_HEIGHT)];
self.view = _myAttentionView;
_tableView = [[RefreshTableView alloc] initWithFrame:_myAttentionView.bounds];
_dataSource = [NSMutableArray array];//do it HERE..........
[self firstGetDataSource];//do it HERE...........
[_tableView registerClass:[kfcFollowCell class] forCellReuseIdentifier:CellIdentifier];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.estimatedRowHeight = SCREEN_HEIGHT - NAV_HEIGHT - TAB_HEIGHT;
_tableView.delegate = self;
_tableView.psDelegate = self;
_tableView.dataSource = nil;
[_myAttentionView addSubview:_tableView];
_tapMyAttentionGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapMyAttentionGesture:)];
[_tableView addGestureRecognizer:_tapMyAttentionGesture];
_canRefreshFooter = YES;

Changing Collection view flow layout

I'm having this issue with switching flow layouts, in the view debugger it changes to the flow layout that I want, however in the simulator and on my phone it's a jumbled mess. I've been looking everywhere for an answer or what the problem is and I can't find a solution. ![left side is what im trying to accomplished and how the view debugger looks, right is the simulator][1]
here is the code for the collectionview and the flow layouts -
(void)setupCollectionView
{
CGRect mainscreen = [[UIScreen mainScreen]bounds];
self.originalLayout = [[UICollectionViewFlowLayout alloc]init];
self.otherLayout = [[UICollectionViewFlowLayout alloc]init];
self.collectionView = [[UICollectionView alloc]initWithFrame:mainscreen collectionViewLayout:self.originalLayout];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:#"Cell"];
[self.collectionView setPagingEnabled:YES];
[self.originalLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.originalLayout setMinimumInteritemSpacing:0.0f];
[self.originalLayout setMinimumLineSpacing:0.0f];
self.originalLayout.itemSize = CGSizeMake(self.collectionView.frame.size.width, self.collectionView.frame.size.height);
[self.otherLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.otherLayout setMinimumInteritemSpacing:10.0f];
[self.otherLayout setMinimumLineSpacing:10.0f];
self.otherLayout.itemSize = CGSizeMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
[self.view addSubview:self.collectionView];
}
-(void)changeLayout
{
if (self.topBar.hidden == NO) {
self.topBar.hidden = YES;
self.bottomBar.hidden = YES;
}
if (self.swapCollectionViewLayout == NO) {
self.swapCollectionViewLayout = YES;
[self.collectionView setPagingEnabled:NO];
[self.collectionView setCollectionViewLayout:self.otherLayout animated:YES];
[self.collectionView reloadData];
}
else
{
[self.collectionView setCollectionViewLayout:self.originalLayout animated:YES];
self.collectionView.pagingEnabled = YES;
self.swapCollectionViewLayout = NO;
[self.collectionView reloadData];
}
Here is a link to a photo of the issue
https://www.facebook.com/photo.php?fbid=909831825747700&l=340de9351b

Custom CollectionViewCell

I create Custom CollectionViewCell with that code and have lags then I use it. Why ?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.webView = [[UIWebView alloc] initWithFrame:CGRectZero];
_webView.scalesPageToFit = YES;
_webView.allowsInlineMediaPlayback = YES;
_webView.mediaPlaybackRequiresUserAction = NO;
_webView.scrollView.scrollEnabled = NO;
_webView.scrollView.bounces = NO;
[self.contentView addSubview:self.webView];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
self.imageView.clipsToBounds = YES;
[self.contentView addSubview:self.imageView];
}
return self;
If I use this code all ok
- (CollectionCell *)collectionView:(IndexedCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
cell.imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
cell.imageView.clipsToBounds = YES;
[cell.contentView addSubview:cell.imageView];
Since you are allocating a webview inside your initialization, I'm going to guess that's why you're seeing the lag.
UIWebView is a heavy allocation to make and since it's within a custom cell, it's probably getting called multiple times. See where I'm going?
As I stated in my comment, it's tough to really know what's going inside your application without some result from Time Profiler.

iOS 7 UITableView inside a popover

I have a UITableView which has some custom styling. This table view appears in two places in the app, one of which is inside a UIPopoverController. However when the tableview is inside the popover it takes on the default tableview styling as stated in the UI Transition Guide under "Popover".
The problem I have is that there appears to be nowhere to change this behaviour. Regardless of where I try and modify properties of the tableview the view inside the popover doesn't change.
Anyone dealt with this issue before or have any ideas?
Here is the init method of LibraryProductView where I create the table view:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.sectionOrdering = [NSArray arrayWithObjects:
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_DESCRIPTION],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_DOCUMENTS],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_ACTIVE_INGREDIENTS],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_RELATED_PRODUCTS],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_RELATED_DOCUMENTS], nil];
self.backgroundColor = [UIColor whiteColor];
self.tableView = [[UITableView alloc] initWithFrame:CGRectInset(self.bounds, 10, 0) style:UITableViewStyleGrouped];
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.separatorColor = [UIColor clearColor];
self.tableView.showsVerticalScrollIndicator = NO;
[self addSubview:self.tableView];
}
return self;
}
Here is where the containing view (LibraryProductView) is added to the popover:
- (IBAction)didTouchInformationButton:(id)sender
{
if (_infoPopover != nil && _infoPopover.isPopoverVisible)
{
[_infoPopover dismissPopoverAnimated:YES];
return;
}
CGSize preferredSize = CGSizeMake(600.0f, 500.0f);
LibraryProductViewController* productController = [[[LibraryProductViewController alloc] initWithPreferredSize:preferredSize] autorelease];
productController.filterByMyCompany = NO;
productController.product = _activityInput.product;
UINavigationController* nav = [[[UINavigationController alloc] initWithRootViewController:productController] autorelease];
nav.title = _activityInput.product.name;
RELEASE(_infoPopover);
_infoPopover = [[UIPopoverController alloc] initWithContentViewController:nav];
_infoPopover.popoverContentSize = CGSizeMake(preferredSize.width, preferredSize.height + 46);
[_infoPopover presentPopoverFromRect:_infoButton.frame inView:_infoButton permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}
The LibraryProductView is created within viewDidLoad method of LibraryProductViewController.
- (void)viewDidLoad
{
[super viewDidLoad];
self.libraryProductView = [[LibraryProductView alloc] initWithFrame:(usingPreferredSize ? CGRectMake(0.0, 0.0, preferredSize.width, preferredSize.height) : self.view.bounds)];
self.libraryProductView.dataSource = self;
self.libraryProductView.delegate = self;
[self.view addSubview:self.libraryProductView];
}
To set properties for the TableView you might do so in
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
[tableView setBackgroundColor:[UIColor redcolor]];
[tableView setSeparatorColor: [UIColor blueColor]];
return 1;
}
This, of course, assumes you have set UITableViewDataSource in your .h file

UITableViewCell subviews issue

I load cell from xib. I added 3 vertical separators to the cell (tried to add from xib and from code). Everything is OK until I select the cell. When I select the sell, separators disappear.
Here are the images:
Not selected:
Selected
Code of adding separators:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.topSeparator = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 1)] autorelease];
[self addSubview:topSeparator];
self.bottomSeparator = [[[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - 1, self.bounds.size.width, 1)] autorelease];
[self addSubview:bottomSeparator];
// Initialization code
CGFloat xOffset = 314;
UIView *imageView1 = [[[UIView alloc] initWithFrame:CGRectMake(xOffset, 0, 1, self.bounds.size.height)] autorelease];
imageView1.tag = kTagBorder1;
imageView1.backgroundColor = [UIColor blackColor];
[self addSubview:imageView1];
xOffset = 487;
imageView1 = [[[UIView alloc] initWithFrame:CGRectMake(xOffset, 0, 1, self.bounds.size.height)] autorelease];
imageView1.tag = kTagBorder2;
imageView1.backgroundColor = [UIColor blackColor];
[self addSubview:imageView1];
xOffset = 573;
imageView1 = [[[UIView alloc] initWithFrame:CGRectMake(xOffset, 0, 1, self.bounds.size.height)] autorelease];
imageView1.tag = kTagBorder3;
imageView1.backgroundColor = [UIColor blackColor];
[self addSubview:imageView1];
}
return self;
}
I even tried this:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
[self bringSubviewToFront:[self viewWithTag:kTagBorder1]];
[self bringSubviewToFront:[self viewWithTag:kTagBorder2]];
[self bringSubviewToFront:[self viewWithTag:kTagBorder3]];
[self setNeedsDisplay];
// Configure the view for the selected state
}
You should not add the lines as a child view of self (UITableViewCell). Instead, add it as a child of self.contentView - which is the view containing the components inside a UITableViewCell.
This article contains tips on how to customize a UITableView properly.
I fixed it. I used UIImageView with black image instead of uiview with black background. In this case view doesn't disappear.

Resources