Grouped UITableView Custom Cell Corners - ios

I have a grouped UITableView with custom cells loaded from nib files. They have the proper 300 px width (for grouped) and appear okay, except for one problem: square corners. The first and the last cell of each group is not appearing to have rounded corners as it should. I have tried setting the backgroundcolor of the tableview to clear, as well as the background of the view. I initialize it in a pretty standard way in my UITableViewController with
self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
I put this in the ViewWillAppear method. I've also tried it in the viewDidLoad method and it made no difference. Any help on getting these corners rounded would be much appreciated! Thanks!

Related

Row number in UITableViewController does not conform the protocol function

I use a dynamic tableViewController. I implement the numberOfSection to return 4, and the numberOfRowInSection return 1. These are the only changes I made for the blank TableViewController. But when I run it on simulator, it shows multiple cells, which I expected to be only four cells. Why is that?
Note: If I change the height of each cell to let the screen only fit four cells at a time, it will work fine since it won't let be me scroll down. So I guess is the tableViewController always try to fill the screen no matter how many cell it should display?
Yes, the UITableView will always continue to fill the screen with 'empty' cells of the default row height. To prevent this, you could add the following code in viewDidLoad:
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
(For Swift, check #VictorSigler's answer.)
In Swift:
self.tableView.tableFooterView = UIView(frame: CGRect.zeroRect)

Adding TableFooterView makes my other views Disappear

My first problem that i had was that my last UITableViewCell never had a separator which i wanted. I solved it using this code:
self.tableView.tableFooterView = [[UIView alloc] init];
Now that worked perfectly however with one problem. when i add that all my other views disappear. Here is a picture of before i use the one line of code above and after:
How can i fix this?
Set a zero height table footer view, like so:
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Because the table thinks there is a footer to show, it doesn't display any cells beyond those you explicitly asked for.
Have you tried to use the viewForFooterInSection function?
Add a vertical spacing of 0 between your table view bottom and the view's top which is placed below it. And you need to set one view's height fixed either for UITableView or UIView. Add this code of line in viewDidLoad. It will display the separator also for last cell.
self.tableView.tableFooterView = [UIView new];
Screenshot:
May be this help
You have to set translucent property of tab bar controller
see this question to more reference...
iOS 7 TabBar Translucent issue

UICollectionView draw lines between the cells

Is there a way to draw lines between the cells in a UICollectionView? I'm not looking to add a border to each cell, that will result in drawing the lines only if there are any cells in the collection. What I'm looking for is something like UITableView's lines: they are there even if the table view has 0 cells.
I solved this by adding views to the collectionView.
I used the cell frames coordinates to determine where I wanted to draw.
However, there is probably a better way of doing this?
You can create a proper background for UITableView if your cells are of the same height. Draw the pattern image and apply it as a backgroundColor property:
UIImage *patternImage = [UIImage imageNamed:#"pattern.png"];
tableView.backgroundColor = [UIColor colorWithPatternImage:patternImage];
UPD:
Comment to this answer in incorrect. Background will scroll with tableView (or scrollView) content.

How to customise the UITableView's Separator View?

I have a problem of the implementation of UITableViewCell's separator.
As you can see from the screenshot, there is a white gap visible before the separator, I think that is because I set the bg colour of the cell as light grey and also I put the inset of the separator as 53.
My first attempt was instead of using the separator, I was trying to draw the lines at the end of the cell by my self. But since on selection of the row the content of the row get updated, and there is a lot of issue regarding the calculation of the height of the cell.
So basically it is really hard for me to draw the line pixel precise at the end of the cell.
This left me the option to access the cell's separator's view, which currently not aware of any easy way, and fill the gap with my default background colour of the table.
My question is, how can I access the separator view
Or
Do I have any other alternatives to implement what I want?
Thank you very much.
UITableView doesn't provide APIs to access or modify the cells separator view directly. However, it has methods to change it's color, style, etc. Most of the times, though, the only solution for a custom separator is to draw it yourself, or better, to set the cell's backgroundView property to a simple view with a line subview or layer in it (a subview, although has some overhead, gives you the flexibility of autoresizing automatically using autoresizing masks or auto layout).
e.g.
UIView *backgroundView = [[UIView alloc] initWithFrame:cell.bounds];
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(backgroundView.bounds) - 1, CGRectGetWidth(backgroundView.bounds), 1.0f];
[lineView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin)];
[backgroundView addSubview:lineView];
cell.backgroundView = backgroundView;
Use this workaround:
Put UIImageView at bottom edge of cell and you can set image as per your requirement
STEP 1: Set divider as per image
STEP 2: Set Separator style to none
Since iOS 7 you can use this
cell.separatorInset = UIEdgeInsetsZero;

How to properly add custom TableViewCell?

I have a grouped tableView in my iPad-app, and I've been trying to set cell.imageView.center = cell.center to center the image instead of putting it to the leftmost position. This is apparently not possible without a subclass of the UITableviewCell(If someone could explain why, that'd also be appreciated.. For now I just assume they are 'private' variables as a Java-developer would call them).
So, I created a custom tableViewCell, but I only want to use this cell in ONE of the rows in this tableView. So in cellForRowAtIndexPath I basically write
cell = [[UITableViewCell alloc]initWith//blahblah
if(indexPath.row == 0)
cell = [[CustomCell alloc]initWith//blahblah
This is of course not exactly what I'm writing, but that's the idea of it.
Now, when I do this, it works, but the first cell in this GROUPED tableView turns out wider than the rest of them without me doing anything in the custom cell. The customCell class hasn't been altered yet. It still has rounded corners though, so it seems it knows it's a grouped tableView.
Also, I've been struggling with programmatically getting the size of a cell, in cellForRowAtIndexPath, I've tried logging out cell.frame.size.width and cell.contentView.frame.size.width, both of them returning 320, when I know they are a lot wider.. Like, all the rows are about 400 wide, and the first cell is 420 or something. It still writes out 320 for all the cells..
This code will not work for a couple of reasons:
cell.imageView.center = cell.center;
Firstly, the center is relative to its superview. I believe the cells superview is the tableView. The imageView's superview will be the content view of the cell. Therefore the coordinate systems are different so the centens will be offset. E.g. the 3rd cell down will have a center of 0.5 widths + 3.5 heights. You should be able to ge around this issue by doing:
cell.imageView.center = CGPointMake( width / 2 , height / 2 );
The second issue is related to how the table view works. The table view manages its cells view's. The width of a cell is defined by the table view's width and the height is defined by the table view's row height property. This means the cell itself has no control over its size.
You can however size its subviews, but you must do this after the cells size has been set (otherwise you can get strange results). You can do this in layout subviews (of the custom UITableViewCell class). See this answer.
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.frame = ....
}
When layoutSubviews is called the cells frame has been set, so do your view logging here instead of cellForRowAtIndexpath.
As for the GROUPED style. Im not sure if this is designed to work with custom views. I suspect it sets the size of its cells to its own width minus a 20 pixel margin on each size, then applies a mask to the top and bottom cells in a section to get the rounded effect. If you are using custom view try to stick with a standard table view style.

Resources