UICollectionView adding UICollectionCell - ios

When I try to put UICollectionCell to UICollectionView in Interface Builder I can't put it with unknown reasons. The cell is going to the tools bar without adding to UICollectionView
I am using:
iOS SDK 6.0
XCode 4.5.1
I don't use Storyboard

Only UICollectionView inside StoryBoard have UICollectionViewCell inside.
If use XIB, create a new XIB with CellName.xib, add CollectionViewCell to it, specify name of UICollectionView custom class. After that use registerNib.
Sample code: https://github.com/lequysang/TestCollectionViewWithXIB

You cannot put UICollectionViewCell directly into the UiCollectionView if you are using Xib file. Its possible only in storyboard. Add a UICollectionViewCell into a separate Xib file. Give your class name. Then register either class or xib before the collection view appears
[self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:CELL_ID];
Typically this is done in viewDidLoad.
This is the implementation of a custom UICollectionViewCell with out using Xib
#implementation CollectionViewCell
#synthesize imageView = _imageView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowRadius = 5.0f;
self.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
self.layer.shadowOpacity = 0.5f;
// Selected background view
UIView *backgroundView = [[UIView alloc]initWithFrame:self.bounds];
backgroundView.layer.borderColor = [[UIColor colorWithRed:0.529 green:0.808 blue:0.922 alpha:1]CGColor];
backgroundView.layer.borderWidth = 10.0f;
self.selectedBackgroundView = backgroundView;
// set content view
CGRect frame = CGRectMake(self.bounds.origin.x+5, self.bounds.origin.y+5, self.bounds.size.width-10, self.bounds.size.height-10);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
self.imageView = imageView;
[imageView release];
self.imageView.contentMode = UIViewContentModeScaleAspectFill ;
self.imageView.clipsToBounds = YES;
[self.contentView addSubview:self.imageView];
}
return self;
}

Okay. There is actually a workaround for this, if you really wanted to have the cell in collectionView inside xib file from interface builder:
Create a storyboard.
Create the UICollectionView and the UICollectionViewCell from interface builder.
Adjust the UI with constraints etc to make it look exactly what you wanted it to be.
Create a new xib file.
Copy everything inside the storyboard to the new xib file.
It will work perfectly.
One thing to keep in mind that step #3 is very important, because after #5 you are not supposed to drag and move around the UICollectionView, if you do, the cell will magically disappear! Other than that, it will just work perfectly.

Related

UITableViewCell width is not adjusting dynamically based on ios device type

My tableview cells are created entirely programmatically (I'm trying to learn to build an app from scratch without using storyboards) and the width of the cells is getting messed up.
Here is a screen shot http://imgur.com/ki6txqg of what the cell looks like in an iPhone 6 Plus. I'm trying to set the cell so that the UIView in the cell(self.view) gets adjusted automatically so that it fills the entire screen. I'm not sure why the width is staying static. Any advice would be greatly appreciated.
-(instancetype)initWithTweet:(PCRTweet *)tweet reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super init];
if (self) {
_tweet = tweet;
reuse = reuseIdentifier;
CGSize cellSize = self.contentView.frame.size;
CGRect backgroundView = CGRectMake(10.f, 5.f, (cellSize.width-20.f), (cellSize.height + 90.f));
self.view = [[UIView alloc] initWithFrame:backgroundView];
self.view.backgroundColor = [UIColor whiteColor];
self.view.layer.cornerRadius = 8;
self.view.layer.masksToBounds = YES;
self.view.layer.borderWidth = 1.0f;
self.view.layer.borderColor = background_color_gray.CGColor;
self.view.layer.masksToBounds = NO;
[self.contentView addSubview:self.view];
CGRect picView = CGRectMake(0.f, 0.f, 65.f, 65.f);
self.contentView.backgroundColor = background_color_gray;
}
return self;
}
Please read the points on following checklist to ensure you doing it all right:
-[ ] Have you checked that your contentView is dynamically changing?
-[ ] Have you tried putting constraints programatically?
-[ ] Try using constraints on the largest view : will auto adjust the relative views
Apart from it, you can auto-resizing for your frame.
You try this two UITableView Delegate method in table view class
For Dynamic Height
#pragma mark - UITableView Delegates
-(CGFloat)tableView:(UITableView )tableView estimatedHeightForRowAtIndexPath:(NSIndexPath )indexPath {
return 44.0;
}
-(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath {
return UITableViewAutomaticDimension;
}
For Width (Get view controller width and take to backgroundView)
CGSize viewWidth = self.contentView.superview.superview.superview.superview.frame.size;
// self.contentView.superview -> return UITableViewCell
// self.contentView.superview.superview -> return UITableViewWrapperView
// self.contentView.superview.superview.superview -> return UITableView
// self.contentView.superview.superview.superview.superview -> return View Controller
CGRect backgroundView = CGRectMake(10.f, 5.f, (viewWidth.width-20.f), (cellSize.height + 90.f));
Try to use auto-resizing for your view.
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth;

Adding a text or drawing to the uitableviewcell

I have code that adds a text label, a subtitle and the accessory icon like so:
cell.textLabel.text = #"Title";
cell.detailTextLabel.text = #"Subtitle";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// And I see code in the docs for an image:
cell.imageView.image = ...
But I want the place where the image goes (on the left) to be a text or lablel (like in Instagram) or a draw circle (like the Apple favorites call screen). How is this done?
Although with UITableViewCellStyleDefault you get imageView property on UITableViewCell for free and you can use it, just in case, you want to have fine control on placement of imageView and labels on cell, you need to go for custom UITableViewCell. Here are the steps on how to achieve this:
Step 1 : Create a new UITableViewCell subclass say MyCustomCell.
#interface MyCustomCell : UITableViewCell
Step 2 : Implement initWithStyle:reuseIdentifier: method in MyCustomCell.m and add any custom view to cell content view.
- (id)initWithStyle:(UITableViewCellStyle)iStyle reuseIdentifier:(NSString *)iReuseIdentifier {
if ((self = [super initWithStyle:iStyle reuseIdentifier:iReuseIdentifier])) {
MyView *myCustomView = [[MyView alloc] init];
myCustomView.frame = CGRectMake(6.0f, 6.0f, 30.0f, 30.0f);
[self.contentView addSubview:myCustomView];
}
return self;
}
Step 3 : Implement layoutSubviews method to have fine control on your cell content subviews.
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(6.0f, 6.0f, 30.0f, 30.0f);
GFloat textLabelXPosition = self.imageView.frame.origin.x + self.imageView.frame.size.width + 10;
self.textLabel.frame = CGRectMake(textLabelXPosition, 0.0, contentRect.size.width - textLabelXPosition, contentRect.size.height);
}
Step 4 : Finally use MyCustomCell instance in you table view controller's cellForRowAtIndexPath: method.
create an imageView image, and then add following lines to your cellForRowAtIndexPath
CGRect newFrame;
newFrame.origin.x = self.accessoryView.frame.origin.x;
newFrame.origin.y = self.accessoryView.frame.origin.y;
self.image.frame= newFrame;
this will give you the access to coordinates of accessory view. Now override with your text/ label.
create A label in your xib - yourLabel
self.yourLabel.frame= newFrame;

Custom UITableViewCell didSelectRowAtIndexPath not being called

I have created a custom UITableViewCell class that I use to draw my UITableViewCell. Everything is drawn correctly however due to the elements I am putting into my UITableViewCell I have been having problems with selecting the cell.
This is the method I use to draw the UITableViewCell which is my custom UITableViewCell
- (void)drawCell
{
nameString = [[UILabel alloc] init];
nameString.backgroundColor = [UIColor redColor];
nameString.frame = CGRectMake(15.0, 0.5, 70.0, 40.0);
nameString.text = [itemsDictionary objectForKey:#"Name"];
lastNameString = [[UILabel alloc] init];
lastNameString.backgroundColor = [UIColor clearColor];
lastNameString.frame = CGRectMake(105.0, 0.5, 95.0, 40.0);
lastNameString.text = [itemsDictionary objectForKey:#"LastName"];
addressString = [[UILabel alloc] init];
addressString.backgroundColor = [UIColor clearColor];
addressString.frame = CGRectMake(220.0, 10.5, addressString.frame.size.width, 50.0);
addressString.text = [NSString stringWithFormat:#"ISN %#: %#",[itemsDictionary objectForKey:#"AddNumber"] ,[itemsDictionary objectForKey:#"AddString"]];
[addressString sizeToFit];
// scrollcell has a dynamic scrollwidth depending on the sddressString but has a framesize of a normal UITableViewCell
scrollCell = [[UIScrollView alloc] init];
scrollCell.backgroundColor = [UIColor blueColor];
scrollCell.frame = CGRectMake(0.0, 0.0, ScreenWidth, 45.0);
[scrollCell setContentSize:(CGSizeMake((220.0 + addressString.frame.size.width)+15, 45.0))];
[scrollCell addSubview:nameString];
[scrollCell addSubview:lastNameString];
[scrollCell addSubview:addressString];
[[self contentView] addSubview:scrollCell];
}
As you can see I am adding a UIScrollView that covers the entire cell which I think is preventing the UITableViewCell delegate selection method.
How can I get the delegate method didSelectRowAtIndexPath to work?
Have you added the following?
#implementation ViewController <UITableViewDelegate, UITableViewDataSource>
and set the delegate to self?
self.tableview.delegate = self;
A UIScrollView in a cell is always going to be a problem because it intercepts events. If you can, rely on the fact that the table view scrolls and make the cell as large as it needs to be to display the content.
If you must have the scroll view there you will probably have to add a view over the top of the cell, add your own gesture recognisers, and choose which events will be sent to the table view and which to the scrollview.
Check out this answer also - you can send touchesBegan/Moved/Ended to nextResponder also.
One of the other two answers is probably the right path but just in case if you are using storyboard segue those will get before didSelectRowAtIndexPath so code you have in there may be irrelevant depending on the segue and what happens.

Hiding UITableView separator behind the contentView(cell.imageView)?

in ios 7 i am using cell built in image view, it behind cell image view but i am try a layout subviews in uitableview custom cell like that
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(5.0f , 5.0f, 50.0f, 50.0f);
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.textLabel.frame = CGRectMake(60.0f, self.textLabel.frame.origin.y, self.textLabel.frame.size.width, self.textLabel.frame.size.height);
}
image view is set correctly but seperator line in imageview side is empty how to seperator line above a imageview or only way to custom imageview
i add the following line in viewdidload
[self.tableviewName setSeparatorInset:UIEdgeInsetsZero];
now seperater line show fully

Calling a UITableViewDelegate method from ViewController

I'd like to call the following method from my view controller class:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
However for the life of me I can't figure out how to do it. I tried:
UIView *view = [tableView viewForHeaderInSection:section];
UIView *view = [self viewForHeaderInSection:section];
UIView *view = [self tableView:viewForHeaderInSection:section];
All give me errors. It's that extra tableView: bit on the beginning. Can anyone give some advice or at least explain what that tableView:(UITableView *)tableView means?
Thanks!
Steve
I don't know why you would want to call it, but if it is implemented in the same object that you're calling it from then you can use self:
UIView* view = [self tableView:tableView viewForHeaderInSection:section];
otherwise, you can get the delegate from the tableView and call the delegate:
id<UITableViewDelegate> theDelegate = tableView.delegate;
UIView* view = [theDelegate tableView:tableView viewForHeaderInSection:section];
Why would you want to call it? This is one of the UITableViewDelegate methods that are normally called automatically when the table is being constructed by the tableView. The tableView class object fills in the parameters that it needs when it makes the call to this method. The view controller only needs to provide the right delegate methods, customized by you, so it can set it up properly.
Did you customize the code, as in this example, in your delegate class?
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)];
// create the button object
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor blackColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:20];
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0);
// If you want to align the header text as centered
// headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0);
headerLabel.text = <Put here whatever you want to display> // i.e. array element
[customView addSubview:headerLabel];
return customView;
}
I'm not near my Mac, or I would give you one of my own examples. But this is the general way this method is used.
By the way, you can see that the parameter tableView is not referenced in the sample code above. If you really want to call this method, use nil. UITableViewDelegate protocol allows the controller to be delegate for more than one tableView. If this happens, the method should test to see which tableView is reference, so that specialized behavior can be accommodated for each tableView.
Additional info:
If you just want to see what the height of your tableView's header is, you can evaluate its sectionHeaderHeight property. There are other properties like this, such as sectionFooterHeight and rowHeight.
You should know that delegate methods are there to help the tableView, using your customization. So the delegate method tableView:heightForHeaderInSection: is actually for you to customize the header height. Your delegate methods tells the tableView what the height is. It isn't a way to examine a property of the tableView.
Apples documentation says that if you customize by using tableView:heightForHeaderInSection: then the tableView sectionHeaderHeight is not valid. You would expect this, because that property refers to the height of all of the section headers.
Using the sectionHeaderHeight property, which you can write to in this case, you can set all of the headers to the same height.
Here is some sample code from something I'm working on now. I've added an NSLog statement for you.
resultsTableVC = [[[ResultsTableVC alloc] initWithController:self] retain];
self.tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 110, self.view.frame.size.width, self.view.frame.size.height-120) style:UITableViewStyleGrouped] retain];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.tableView.autoresizesSubviews = YES;
self.tableView.layer.cornerRadius = 10.0f;
self.tableView.delegate = resultsTableVC;
self.tableView.dataSource = resultsTableVC;
self.tableView.backgroundView = nil;
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.separatorColor = [UIColor defaultResultTableBackgroundColor];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
[self.view addSubview:self.tableView];
NSLog(#"header: %f, row: %f", tableView.sectionHeaderHeight, tableView.rowHeight);
(Someone will probably point out that I don't need some of those retains. I'm still working on that.)
This tells me that the standard section height is 100 and the standard row height is 44.0. I have a pointer to my tableView, a property that I can use through this class.
Now if you are setting the header height using tableView:heightForHeaderInSection: then you should have the height already calculated in your program. I don't think you can query for the height of a particular section (or row) once you set it.
Does this help?

Resources