Add line to the bottom of last cell - ios

I have UiTableView. Cells on it by clicking change itself height. I need to add custom separator lines. I alwaays add to the top of cell. So all work fine, but last cell haven't seporator line on the bottom, and user cann't see the end of tableview. I try to add next code in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
and then try use it in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
I create in the begin new UIImageView
#implementation MovieSessionsVC
...
UIImageView *seporatorView;
}
And this my code:
[seporatorView removeFromSuperview];
if (indexPath.row == self.sessions.count - 1) {
seporatorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"seporator.png"]];
if([self cellIsSelected:indexPath])
seporatorView.frame = CGRectMake(0, 106, cell.width, 0.5);
else
seporatorView.frame = CGRectMake(0, 63, cell.width, 0.5);
[cell addSubview:seporatorView];
}
I use self.sessions.count for number of rows, and heoght of my cells is 64 and 107 when selected
So line appear and work fine, but when I pick up my tableview on maximum it is disappear

If it isonly the last cell where you want to add something to, a line in your case, then it is probalby the easiest to draw the line on top of the footer view. You can create a UIView and assign that to the tableFooterView property of UITableView. That footer may just have a height of 1.0 and is filled with a colour.
You can of course draw a line in drawInRect of that footerView. For doing so you will need to subclass UIView. But I think that some standard UIView with a background colour and a height of 1.0 would do.

Just set the resize property of the separator image same as the image below
The separator image to sit to the bottom on any cell resize.. Make sure the cell resize property is all selected..

Related

Image on cells not all the way to the left

I am learning about UITableview on iOS and following a course online. I get the table showing fine, but the images on my cells are not all the way to the left (whereas the instructor's ones are). Here is a screenshot of the cells in question:
I don't want that gap, I want the images to be positioned right at the beggining of the cell, all the way to the left. I have done some research and it seems Apple has changed the default look of the cells between ios6 and ios7 so that now the images in cells show a little gap at the left. To get rid of it, I have tried UIEdgeInsets:
[tableView setSeparatorInset:UIEdgeInsetsZero];
and that's not working. I also have tried this approach:
cell.imageView.frame = CGRectMake( 0, 0, 50, 55 );
Nothing happens. So how would I go about it? Thanks
edit-------------------------------------------------------------------------------------------------------------------------------
Still not have found the answer to this. The solutions posted here don't work. I found this piece of code:
self.tableView.contentInset = UIEdgeInsetsMake(0, -50, 0, 0);
Which besides completely puzzling me (as the parameter affected should be the y?) I thought solved the issue by making the image on the cell appear all the way to the left, until I realised it only moved the whole view to the left (as I should have expected I guess) leaving an equal gap on the other side of the screen. All I want is for my images in the cells to appear all the way to the left of the cell as it used to be the case on previous ios. Thanks
It happens because default table content offset from left is 15, you should change it with 0.
See this once, you get idea Remove empty space before cells in UITableView
If you create custom cells. UITableViewCell have owner imageView. Change title of image in your cell.
If you use default cell, use custom cell with constraint Leading space = 0.
It is better not use default imageView of the cell. Drag and drop UIImageView from objective library, create a custom table view cell (Child class of UITableViewCell) then create and outlet of the image view just dragged.
The spacing in the UITableViewCell is because of the default TRUE returned by shouldIndentWhileEditingRowAtIndexPath method of UITableViewDelegate.
I was able to reproduce your problem by the below scenario:
UITableView is in editable mode:
self.tableView.editing = true
And you have implemented:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
To correct your code:
If you do not want to set Editing Style then you can turn off the editing mode by
self.tableView.editing = false
and remove editingStyleForRowAtIndexPath.
Else if you need editing mode then set the appropiate Editing style(UITableViewCellEditingStyleDeleteor UITableViewCellEditingStyleInsert) or simply turn the indentation off.
- (BOOL)tableView:(UITableView *)tableView
shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return FALSE;
}
You must create a custom cell, by adding a new class as a subclass of UITableViewCell. then you can design cell with autolayout and constraints which will resolve the issue.
there is a another concrete way to achieve this by creating subclass uitableviewcell (custom class).
steps to follow
create a class subclass of UITableViewCell.
in .h file create properties and outlets of UI components.
go to storyboard and add table view cell inside the tableview.
now add UI components like: imageview or button etc and set the x, y values according to.
make class of custom cell your className using identity inspector see image.
connect all outlets of UI components.
use below code uitableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *MyIdentifier = #"uniqueIdentifire";
yourCustomClassForCell *cell = (yourCustomClassForCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil){
cell = [[yourCustomClassForCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
}
cell.imageView.image = [imageAry objectAtIndex:indexPath.row];
}
Dont forget to give identifire by selecting your cell using storyboard Attribute inspector uniqueIdentifire to identifire property see image.
Also you can give some vertical space between cells by just to add this below code (Method only) inside customeCellClass.
- (void)setFrame:(CGRect)frame { // method to insert gap between table view cell
frame.origin.y += 6;
frame.size.height -= 2 * 6;
[super setFrame:frame];
}
You can not really change the frame of the inbuilt subviews of uitableviewcell like imageview, accessoryview. But if you create a custom tableviewcell class(even if you do not add any other subelement to it), you can change the frame of the inbuilt imageview by overriding the layoutSubviews method inside the UITableViewCell. I have tried it and it works.
#import "TableViewCell.h"
#implementation TableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void) layoutSubviews{
[super layoutSubviews];
CGRect frame = self.imageView.frame;
frame.origin.x = 0;
self.imageView.frame = frame;
}
#end

How do I get the default cell separator color? Also, how do I set separator color per cell?

I want to add a cell in the bottom that shows only if the app is loading more results when the user scrolls to the bottom of a tableview. I already did that. The problem is that it's a cell. So when I scroll down even further, the separator shows that it's just another cell in the table. I want to hide that. I have to change that cell's separator color, how do I do that to a particular cell? Also, Where/when do i do that?
I realize that if I change that cell's color (let's say it's #5), when I get the results and load them in, won't that also result in cell #5 still having an invisible separator? So how do I set it back to the default color? More particularly how do I get the default color for a tableviewcellseparator programmatically?
Thanks
Here's a simple trick: you have to add en empty UIView inside your UITableView in Storyboard. The set the height = 0.
The last separator of the UITableView magically disappears.
Edit:
You will also need to edit your cellForRowAtIndexPath: like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"identifier"];
if (indexPath.row == [self tableView:tableView numberOfRowsInSection:[self numberOfSectionsInTableView:tableView]-1]-1) {
//this is the last cell
cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0);
} else {
cell.separatorInset = UIEdgeInsetsMake(0, 0, cell.bounds.size.width, 0);
}
return cell;
}
Separator colour is a property of the table view, you can get or set it like so:
self.tableView.separatorColor = UIColor.blackColor()
But I don't think you cannot change the separator colours of a individual cell.
If you truly need that level of customization, you should probably just draw the separator bar yourself as, for example, a thin UIView at the top of the cell. Then you can change its appearance to whatever you want.

How to dynamically change the layout of my cell in TableView

I have a TableView and I implemented this method to return a Cell for each row:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
And in my Cell, I have a ImageView to the left of the cell, follow by a Label to the right of the ImageView.
However, in come case, I want to make the ImageView disappears and have just a Label and move the Label to where the ImageView were.
So I add the following condition in the cellForRowAtIndexPath method
if (//imageView should disappear)
{
cell.image.frame = CGRectMake(cell.image.frame.origin.x,
cell.image.frame.origin.y,
0, 0); // set widht and height of image to 0.
cell.label.frame= CGRectMake(cell.image.frame.origin.x,
cell.image.frame.origin.y,
cell.label.frame.size.width,
cell.label.frame.size.height);
// move the label to where the image were.
[cell layoutIfNeeded];
}
I step thru the code in the debugger, it does get executed. But when I run it, nothing get changed. The image view still there and the Label still to the right of Image View.
Can you please tell me what am I missing?
Thank you.

Change view position when configuring cell. Strange behavior

I have a standard UINavigationController with a UITableViewController at it's root. In IB, I paint a prototype cell with a label and a UIView. The UIView contains a button. I'd like the UIView to be x-aligned after the label, as a function of the length of text in the label.
In IB, the view's left side is initially aligned with the label's left side. There are no layout constraints in IB.
Here's my cellForRowAtIndexPath ...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSArray *labels = #[#"SOME STRING", #"SHORTER", #"A VERY MUCH LONGER ONE", #"REGULAR ONE", #"TINY"];
UILabel *label = (UILabel *)[cell viewWithTag:32];
label.text = labels[indexPath.row];
UIView *view = [cell viewWithTag:33];
CGSize size = [label.text sizeWithAttributes:#{NSFontAttributeName:label.font}];
view.frame = CGRectOffset(label.frame, size.width, 0);
return cell;
}
Two problems: I've set breakpoints and watch this code running the first time the view appears. I see the view.frame get changed for each row, but the view does not change position. I remains in it's IB-position, right on top of the label. If I scroll the table down, the views on lower cells (presumably reused) are in the desired position. If I scroll back up, the upper rows are also good. It just fails to work on the initial presentation of the upper cells.
Second problem is that the button contained in only the first row has a subtle, strange effect applied to it's text, like a blur. See attached...
First Row Button (zoomed in mac preview... see that extra blur on the left edge of the letters?)
Other Row Buttons
Stuff I tried:
I've tried a few variations, including using a regular view controller with a table view added (rather than a UITableViewController). I've found that if I reloadData on viewDidAppear, that solves the placement problem, but not the blurry button. (Also, I don't like the idea of needing to reload on viewDidAppear). Doing so on viewWillAppear has no effect at all. I've also tried animating the label change slowly. It happens, but again, only on the second time the cell is configured. I try changing the UIView color to prove the code is being run. The color change happens every time, including the first time, but not the view placement. Am I nuts?
For problem 1:
Try calling [cell layoutIfNeeded] before returning the cell, there should be no performance hit when it does not need relayout.
For problem 2:
try calling CGRectIntegral before you set the frame. ie
view.frame = CGRectIntegral( CGRectOffset(label.frame, size.width, 0) );

Remove separator in grouped UITableView

I need to make a grouped table view with custom cells. Each cell must have a background image, so that image of one cell will touch the image of the second cell and so on. I've tried to set the separatorStyle to None, but I still get the transparent separator between cells.
Help me, please, remove this space between cells.
Have a good day,
Thanks!
I found that when I set the background image, the separator went away automatically. The problem is that I want it to show up.
Here's what I did:
Create a custom UITableViewCell class. In the respective init method do:
// Create a background image view.
self.backgroundView = [[UIImageView alloc] init];
Then in your controller that manages the UITableView:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// After all of the cell dequeue/allocation...
UIImageView *backgroundView = (UIImageView *)cell.backgroundView;
backgroundView.image = [UIImage imageNamed:#"your_image"];
}

Resources