I have a UITableViewCell which contains an UIView.
This UIView is larger than the UITableViewCell and should also be visible in the cell above.
clipsToBounds=NO
did not work.
This does only work when I remove the content from the upper cell and set the background color to clear.
This means, the content in my upper cell covers my UIView, that's why I tried to call bringSubViewToFront, but this did also not work.
What am I doing wrong?
This is how I add the custom UIView to the cell's contentView:
CustomView *view=[[CustomView alloc] initWithFrame:CGRectMake(0, 0, [DeviceInformation getWidth], [self tableView:tableView heightForRowAtIndexPath:indexPath])];
[cell.contentView addSubview:view];
and in my CustomView I am adding another UIView, which should be then presented among multiple Cells.
Thanks!
Related
I change the frame of cells (add margins to the left and to the right) according to the code. The problem is cells update their frames only after they disappear and appear again via scrolling. I used table view's - reloadData as well, but it did't help. How do I force cells to be redrawn without scrolling?
- (UITableViewCell *) tableView: (UITableView *) tableView
cellForRowAtIndexPath: (NSIndexPath *) indexPath {
PersonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: #"TableViewCellID"
forIndexPath: indexPath];
cell.frame = CGRectMake(20, cell.frame.origin.y, cell.frame.size.width-2*20, cell.frame.size.height);
/* tried any of those
[cell setNeedsDisplay];
[cell setNeedsLayout];
[cell layoutIfNeeded];
*/
return cell;
}
I'd recommend using .xibs for this. Much easier.
As far as I know, you are not able to change the frame of a cell in that manner. The cell's height is determined by heightForRowAtIndexPath, and the width is set to the width of the tableView.
You may be able to do it in some manner the way you are attempting, but the cleanest way I know is the following.
If you want there to be a margin around the cell, you can:
Create a nib for a UITableViewCell with a UIView containing all your views, and place a border using constraints.
Embed all your content inside a UIView (lets call this borderedContentView) and place this as the immediate subview of contentView
Place constraints relating borderedContentView to the contentView, with the leading, trailing, top and bottom constraints set to the values that create the border width you desire.
If your tableView has a backgroundColor, you'll have to set the contentView's backgroundColor to the same color as the tableView's backgroundColor so as to create the illusion of a margin. Do this in the tableView's delegate method willDisplayCell: or in a subclass of UITableViewCell awakeFromNib or other related method.
Bask in the glory of your margined cells.
You can also do this programmatically if you prefer not to use interface builder, but it is very easy to do in IB.
Hope this helps.
The solution to the problem is to override setFrame method of UITableViewCell. This way it perfectly works.
I have tall tableView and I want to add view on it, I want that it too will be tall.
I add view with this code but it only in one view height - 568 points.
_backgroundSelectionView = [[UIView alloc]initWithFrame:self.tableView.bounds];
_backgroundSelectionView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
[self.tableView addSubview:_backgroundSelectionView];
How can I add view on all screen?
UITableViewController provides UITableView and you cannot add subviews to tableViewController on top of tableview easliy.
Instead of using UITableViewController change it to UIViewController(which will conforms to UITableViewDataSource and UITableViewDelegate), with this you can add views(tableView and any other subView) on main UIView of UIViewController.
I am trying to display cell's textLabel text to be visible on top of an image that spans the entire cell. I created a custom cell subclass just to make the image width be equal to the entire cell size like so:
- (void) layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(0,0,320,80);
}
Now I would like to display my cells text on top of the cell. I do not have the cell text in the custom cell subclass, rather the code for it is in the TableViewController. Is there a way I can make the text display on top from within the view controller or do I have to put the text to be displayed in the custom cell subclass? Thank you
The best way is to make a custom cell, with an imageView and a label, and add them to the subview when the cell is initialized.
Then make sure they're hooked up to a property so you can configure the frame, as well as the content.
You can display the text in the TableViewController. Just make sure you set the image first before you set the text. I have provided code snippet in the method tableView:cellForRowAtIndexPath:
cell.imageView.image = theImage;
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 0.0,100.0, 30.0)];
textLabel.text = #"Testing";
[cell.contentView addSubview:textLabel];
I am doing a slide menu using a UITableView and I have 2 options on the menu and I want to put a button at the bottom like in this image:
I try to do that add a tableFooterView like that.
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 500, 320, 70)];
footerView.backgroundColor = [UIColor blackColor];
self.tableView.tableFooterView = footerView;
However, the view appears just after the second cell, but I want it at the bottom.
Thanks for help.
No you shouldn't add any empty cells, that's just hacky. If you really need the button to be at the bottom, you should use layoutSubviews to control the frame of the tableView and the footerView.
- (void)layoutSubviews
{
[super layoutSubviews];
self.tableView.frame = // top 80% of the screen
self.footerView.frame = // bottom 20% of the screen
}
You should know that every UITableViewCell has its height and footer is part of a UITableView and will appear at the bottom of a UITableView. If you want to make your UITableView look like what that image shows, you should make sure that your cells are high enough to make sure that your UITableView are high enough so that footer will appear at the bottom of UITableView
My suggestion is to add extra "empty" cell(I mean a cell with no content but has a height).
Add a Container View with View Controller from storyboard. You can use autoresizing to set the buttons on right place.
In a UITableViewController I am instantiating UITableViewCells where some cells are highlighted by an accessoryView. For me, this works:
// works for me
UIImageView *favoriteImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon"]];
[cell setAccessoryView:favoriteImageView];
It seems wasteful to me to instantiate the same view repeatedly for multiple cells; however when I attempt to re-use the same view as the accessoryView of multiple cells my app fails in a miserable way (completely black screen, no views presented) I haven't been able to debug. Whether I declare favoriteImageView as a static inside the method such as
// doesn't work for me
static UIImageView *favoriteImageView = nil;
if (!favoriteImageView)
favoriteImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon"]];
[cell setAccessoryView:favoriteImageView];
Or declare it as an ivar and define it in init such that I wind up with:
// doesn't work for me
[cell setAccessoryView:[self favoriteImageView]];
In these two not-working cases, when one cell has its accessoryView set, it displays properly. As soon as I mark a second row such that the accessoryView would be set to reference the same view, the whole thing hangs up.
What are the requirements for constructing a UIView and/or configuring a UITableViewCell in such a way that the same UIView may be referenced as the accessoryView of multiple UITableViewCells?
UIImageView extends from UIView. And a UIView can't be in two or more places at the same time.
Therefor, if you try to display an UIImageView in two or more cells at the same time, it won't work. You need an UIImageView for each cell on screen.
I suggest that for each cell you create the UIImageView. The tableviewcells will be reused alongside with their accessoryview, so I wouldn't worry too much about performance or memoryproblems.