Adding a text or drawing to the uitableviewcell - ios

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;

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;

iOS - Facebook POP: slide tableview cell to bottom right corner

Maybe you can help me with a problem.
I'm using the new Facebook POP animation framework in an iOS (7) app.
I have a tableview with a "+ button" in each cell. I want that if a user clicks on a button in the cell, that the cell (a copy of that cell) slides to the bottom right corner (in the last tabbar item) from alpha 1 to 0. Like a "add to cart" button. So the user knows that the row is added to the cart.
Does anyone know how I can accomplish that with the Facebook POP framework? Or can you point me in the right direction?
I think it's not so difficult, but I can't figure it out.
Many thanks in advance!
Assuming you refer to a UITableView inside a UIViewController which is assigned as a tab of a UITabBarController, first you duplicate the selected cell into a UIView and then you perform basic POP animation as follows:
#import <math.h>
#import "POPAnimation.h"
#import "POPBasicAnimation.h"
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIView *view = [self duplicateCell:cell
ContentOffset:tableView.contentOffset
Row:indexPath.row];
[self.view addSubview:view];
NSUInteger numberOfTabs = 3;
CGFloat tabWidth = self.view.frame.size.width / numberOfTabs;
[self fadeOutView:view WhileMoveTo:CGRectMake(self.view.frame.size.width - tabWidth,
tableView.frame.size.height,
tabWidth,
view.frame.size.height)];
}
- (UIView*)duplicateCell:(UITableViewCell*)cell ContentOffset:(CGPoint)offset Row:(NSInteger)row
{
CGFloat cellHeight = cell.frame.size.height;
CGFloat topVisibleCellRow = (int)(offset.y / cellHeight) ;
CGFloat delta = fmodf(offset.y, cellHeight);
CGRect frame = cell.frame;
frame.origin.y = (row - topVisibleCellRow)*cellHeight - delta;
UIView *view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [UIColor yellowColor];
UILabel *label = [[UILabel alloc] initWithFrame:cell.textLabel.frame];
label.textColor = [UIColor blackColor];
label.text = cell.textLabel.text;
label.contentMode = UIViewContentModeScaleAspectFit;
[view addSubview:label];
return view;
}
- (void)fadeOutView:(UIView*)view WhileMoveTo:(CGRect)rect
{
[view pop_removeAllAnimations];
POPBasicAnimation *animFrame = [POPBasicAnimation animationWithPropertyNamed:kPOPViewFrame];
POPBasicAnimation *animAlpha = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
CGFloat fDuration = 1.5f;
animFrame.toValue = [NSValue valueWithCGRect:rect];
animFrame.duration = fDuration;
animAlpha.toValue = #(0.0);
animAlpha.duration = fDuration;
[view pop_addAnimation:animFrame forKey:#"animateFrame"];
[view pop_addAnimation:animAlpha forKey:#"animateAlpha"];
}
This example is based on a basic UITableViewCell but you can adapt the cell duplication to any custom cell scheme.

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

UICollectionView adding UICollectionCell

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.

Resources