I have a tableview with a header view on it and paging enabled:
[super viewDidLoad];
CGRect headerFrame = [UIScreen mainScreen].bounds;
UIView *header = [[UIView alloc] initWithFrame:headerFrame];
header.backgroundColor = [UIColor flatWhiteColor];
self.tableView.tableHeaderView = header;
self.tableView.pagingEnabled = YES;
The Cells are the height of the screen:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [UIScreen mainScreen].bounds.size.height;
}
however, when I run it on my iPhone 6, the first table cell is never highlighted properly:
when its not highlighted
when I select the cell
help anyone?
The problem is because the height of headerView it's not actually equal with the size of the tableView and on the next page(the first table cell) you are seeing a part from the headerView.
Try this code:
CGRect headerFrame = CGRectMake(0, 0, self.tableView.frame.size.width, self.tableView.frame.size.height);
and
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return self.tableView.frame.size.height;
}
I found out the answer. It turns out a UIView inside my header was a bit too long and overlapped into my table cell. Thanks for your answers!
Related
So I am going to custom UITableViewCell selected background view. Here what I have done:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
CGRect cellFrame = [tableView rectForRowAtIndexPath:indexPath];
CGRect selectedBackgroundFrame = cellFrame;
UIView *selectedBackgroundView = [[UIView alloc] init];
selectedBackgroundFrame.size.height = 5;
[selectedBackgroundView setFrame:selectedBackgroundFrame];
selectedBackgroundView.backgroundColor = [UIColor colorWithRed:180/255.0
green:138/255.0
blue:171/255.0
alpha:1];
NSLog(#"%f %f %f %f",
selectedBackgroundFrame.size.width,
selectedBackgroundFrame.size.height,
selectedBackgroundFrame.origin.x,
selectedBackgroundFrame.origin.y);
cell.selectedBackgroundView = selectedBackgroundView;
return cell;
}
But the result is the selected background view still populates whole cell, instead of my expectation which is height 5.
It also prints the right thing 320.000000 5.000000 0.000000 0.000000. What's wrong?
You might missed below lines in
ViewDidLoad While inserting new row, make height consistence.:
self.tableView.estimatedRowHeight = kCellHeight; // Your custom height for cell
self.tableView.rowHeight = UITableViewAutomaticDimension;
And also,
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return kCellHeight;
}
This is probably because something somewhere is setting an autoresizingMask on the view. Maybe try setting it to flexibleWidth and not setting flexibleHeight in cellForRow
I have a uitableview with a tablefooterview. I would like use cell separators within the tableview but would like to get rid of the full screen width separator placed by default between the tableview and the tablefooterview. I have tried
if ([cell respondsToSelector:#selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
and
cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0);
but neither works
Have a look at this sample implementation.
You can see here that UITableView with plain style does in fact loose the last separator if a footerView is set.
(Orange view is the footerView and there is no separator above him)
Of course in Grouped tableView this does not work as discussed in comments under your question.
https://gist.github.com/bartekchlebek/2b05b5ddcc3efec3f514
#import "ViewController.h"
#interface ViewController () <UITableViewDataSource>
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds
style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
footerView.backgroundColor = [UIColor orangeColor];
tableView.tableFooterView = footerView;
[self.view addSubview:tableView];
}
#pragma mark - UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"ID"];
cell.textLabel.text = [NSString stringWithFormat:#"Row: %#", #(indexPath.row)];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
#end
Edit:
I have found that a much simpler way to avoid the gap between the last section and the table footer is to implement heightForFooterInSection and return a very small value that is not 0. This will result in nothing being visible.
For some reason, returning 0 doesn't suppress the rendering of section footers in a grouped-style table view, and it will try to render a 1-point footer. There is other discussion on StackOverflow about this.
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return CGFLOAT_MIN;
}
I was able to accomplish this in what feels like a slightly hacky way, but it seems to work fine.
My hack is simply to place the actual footer view you want inside of a wrapper view, with a frame position of (0, -1).
Let's say you have a table footer view that you want to use, a class called MyTableFooterView.
In that case you can do something like this:
//This is the view we will actually add as the table footer
//We make it one point shorter since the content will be shifted up
//one point out of its frame
CGRect footerFrame = CGRectMake(0, 0, self.tableView.width, myFooterHeight-1);
UIView* tableFooterWrapper = [[UIView alloc] initWithFrame:footerFrame];
//This is the view that we are trying to use as a table footer.
//We place it 1 point up inside the wrapper so it overlaps that pesky 1-point separator.
CGRect contentFrame = CGRectMake(0, -1, self.tableView.width, myFooterHeight);
UIView* footerContent = [[MyTableFooterView alloc] initWithFrame:contentFrame];
//Make sure the footer expands width for to other screen sizes
footerContent.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[tableFooterWrapper addSubview:footerContent];
self.tableView.tableFooterView = tableFooterWrapper;
This works reliably for me. It's possible that there's a more idiomatic way, but I haven't come across one yet.
Use following code
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
We have a UITableView and it was working fine for iOS7 but when I tried in the iOS8 simulator, I saw the cell separator was placed incorrectly for most cells. Strangely, some cells display the separator correctly.
See the attached image:
I set the cell height this way:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70.0f;
}
Do you know what's happening? Thanks!
EDIT:
A workaround I'm using in the meantime is to disable the separator...
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
...and paint it myself in the cell view...
UIView* separator = [[UIView alloc] initWithFrame:CGRectMake(20, cellHeight-1, frame.size.width - 40, 1)];
separator.layer.borderWidth = 1;
separator.layer.borderColor = [[UIColor grayColor] CGColor];
[self addSubview:separator];
...but I'd like not to do it.
I had the same problem and here is how I solved it:
If you are using a custom cell for your table view and you have overridden layoutSubviews, you need to call it's superview's method at the beginning of the method:
- (void)layoutSubviews
{
[super layoutSubviews];
// Your implementation
}
Hey everybody :) i try to change the width of my cells so there is a little space between the cells and the tableview border. I tried everything i could found here on stackoverflow, but without success.
I created the tableview with the interface builder, so first i simple tried to set the tableview size to "freeform" and dragged the width to 300.0f, but nothing happenend. Than i tried to do it programmatically in my "viewDidLoad" with:
self.tableView.frame = CGRectMake(10.0f, self.tableView.frame.origin.y, 300.0f, self.tableView.frame.size.height);
but here also nothing happens.... than i tried to change the cells directly with:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
newsCell.contentView.frame = CGRectMake(10.0f, 0, 300, newsCell.frame.size.height);
}
but same Problem here....any ideas what I missing?
EDIT: Another Solution for this Problem is to change the frame of the Custom Cell with:
- (void)setFrame:(CGRect)frame {
frame.origin.x += inset;
frame.size.width -= 2 * inset;
[super setFrame:frame];
}
just try this
in your custom cell put a property like
in .h file
#interface GTNewsCustomCell : UITableViewCell
#property (nonatomic, assign)CGRect cellFrame;
in .m file
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.backgroundColor = [UIColor greenColor];//for testing purpose only
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
//automatically called
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect cellRect = self.bounds;
cellRect.size.width = self.cellFrame.size.width;
self.bounds = cellRect;
}
.in .m of viewController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
GTNewsCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if(cell == nil)
{
cell = [[GTNewsCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
cell.cellFrame = CGRectMake(10, 0, tableRect.size.width,40);//hear tableRect is the frame of your tableview
return cell;
}
not sure try this hope this helps u
For this, first of all you can take an UIImageView to cover your full view and set its image as a bordered image. Now add a table view on this imageview with making width so as the borders of this image is visible.
I think you want dynamic Height for the Tableviewcell instead of width.
Delegate method of UITableView will help on this:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Which will return height of every cell. You can implement it as following sample code. This is showing dynamic height on the basis of dynamic text content.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//set width depending on device orientation
self.cellPrototype.frame = CGRectMake(self.cellPrototype.frame.origin.x, self.cellPrototype.frame.origin.y, tableView.frame.size.width, self.cellPrototype.frame.size.height);
CGFloat quotationLabelHeight = [self sizeOfLabel:self.cellPrototype.quotationLabel withText:[self quotationTextForRow:indexPath.row]].height;
CGFloat attributionLabelHeight = [self sizeOfLabel:self.cellPrototype.attributionLabel withText:[self attributionTextForRow:indexPath.row]].height;
CGFloat padding = self.cellPrototype.quotationLabel.frame.origin.y;
CGFloat combinedHeight = padding + quotationLabelHeight + padding/2 + attributionLabelHeight + padding;
CGFloat minHeight = padding + self.cellPrototype.avatarButton.frame.size.height + padding;
return MAX(combinedHeight, minHeight);
}
You can try with this too.
Use this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
delegate method of UITableView and return a float value (CellHight+space bw cells).
So my UITableView has a header, which is the UIImageView shown, and comments below the image. I am trying to increase the space between the image and the comments table.
(I have tried increasing the height of the header, but it doesn't work in my case because it will result in a bigger UIImageView and the image won't cover the view completely)
I experimented with this hack:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CommentsTableCell";
CommentsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Comment *comment = [self.comments objectAtIndex:indexPath.row];
[cell setUsername:comment.handle andText:comment.text];
/* Dirty hack:
1. We cannot increase the height of the header because that will leave spaces in the image.
2. The only way we can increase the margin from the comments table to the picture is by
increasing the individual inset of the first and last comments cell
*/
if (indexPath.row == 0) {
[cell setContentInset:UIEdgeInsetsMake(COMMENTS_PADDING * 10 , 0, 0, 0)];
} else if (indexPath.row == [self.comments count] - 1) {
[cell setContentInset:UIEdgeInsetsMake(0, 0, COMMENTS_PADDING * 10 , 0)];
}
return cell;
}
and in my CommentsCell.m:
- (void)awakeFromNib {
self.commentText.scrollEnabled = false;
self.commentText.editable = false;
self.commentText.contentInset = UIEdgeInsetsMake(-1 * COMMENTS_PADDING, 0, 0, 0);
}
- (void)setUsername:(NSString *)username andText:(NSString *)text {
[self.commentText setAttributedText:[CommentsCell getContentStringForUsername:username andText:text]];
}
- (void)setContentInset:(UIEdgeInsets)inset {
self.commentText.contentInset = inset;
}
but the first comment still has the same inset. I checked the debugger and awakeFromNib is occurring before cellForRowAtIndexPath. Do you see why my method is not working?
I am also open to other suggestions.
You should be able to add some space to the header view just below the image you display. Instead of setting the table's header view to a UIImageView, why not create a container view that you can add the image view to and then just have some space below it.
- (UIView *) buildTableHeaderView {
UIImage *image = [UIImage imageNamed: #"my_image.png"];
CGFloat height = image.size.height + 20;
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, self.myTableView.frame.size.width, height)];
[imageView setImage: image];
UIView *headerView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.myTableView.bounds.size.width, height)];
[headerView addSubview: imageView];
return headerView;
}
What you can do is create a custom UIView (with .xib if you want for easier UI design) with a space on the bottom and return it from - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section method of the UITableViewDelegate, also don't forget to return the height for the header view by implementing the - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section method of the same UITableViewDelegate.
Here is a short example:
-UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
YourCustomHeaderView *headerView = [YourCustomHeaderView instantiateView]; //static method (you can rename it) that will load the custom view from a .xib
//do aditional UI setup or not
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return DEFAULT_HEADER_HEIGHT; //a defined value
}
If you are having a single header view, then you should use the same custom header view creation/init/setup, but move your table downwards in is superview and add the custom header view at the top at any position you like.