Hi i am new for ios and in my app i have created one UITableView and i have set background image for UITableViewcell but image not filling the whole width of screen as like below screen. Why this problem is occuring?
I mean UITableViewCell left and right sides gap is coming images is not filling whole cell width.
please help me someone
my code:-
#import "TableViewController.h"
#interface TableViewController ()
{
UITableView * tableList;
TableCell * Cell;
}
#end
#implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
tableList = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen]bounds].size.width, [[UIScreen mainScreen]bounds].size.height) style:UITableViewStylePlain];
tableList.delegate = self;
tableList.dataSource = self;
tableList.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:tableList];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"MyCell";
Cell = (TableCell *)[tableList dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (Cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableCell" owner:self options:nil];
Cell = [nib objectAtIndex:0];
}
//UIImageView *imageBackground = [[UIImageView alloc] init];
if (indexPath.row == 0) {
Cell.backGroundImage.image = [UIImage imageNamed:#"cell_top.png"];
} else if (indexPath.row == 9) {
Cell.backGroundImage.image = [UIImage imageNamed:#"cell_bottom.png"];
} else {
Cell.backGroundImage.image = [UIImage imageNamed:#"cell_middle.png"];
}
//imageBackground.contentMode = UIViewContentModeScaleToFill;
//Cell.backgroundView = imageBackground;
return Cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 44.0;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([cell respondsToSelector:#selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:#selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
if ([cell respondsToSelector:#selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
#end
Try to set the layoutMargins property of the cells and the UITableView to UIEdgeInsetsZero.
- (void) viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
tableList.layoutMargins = UIEdgeInsetsZero;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[...]
Cell.layoutMargins = UIEdgeInsetsZero;
return Cell;
}
Also check for the contentMode of the UIImageview.
Cell.backGroundImage.contentMode = UIViewContentModeScaleAspectFill;
try set contentInset on Left = 0
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
Use Debug View Hierarchy to figure out UITableView, UITableViewCell or UIImage is not filling the whole width of screen
http://www.raywenderlich.com/98356/view-debugging-in-xcode-6
Please check your "TableCell" in the storyboard. Did you select custom insets option for your custom cell?
Rather than setting up your table view with code, you want to do this in a storyboard. Then you'll want to use auto layout to connect constraints from the table view to the view controller's view. There are lots of tutorials available to teach you how to do this. Learning this will make things much easier in the long run.
Change the name of your tableList property to tableView. That will make more sense to other developers (including yourself in the future), since that's what it is (a UITableView instance).
Your cell is named Cell with a capital C, but you don't want to name properties with capital letters. Also, it doesn't need to be a class property the way it's being used. Remove it from the #interface section.
Coding Guidelines for Cocoa
Remove the -numberOfSectionsInTableView: method. The default is 1, so you don't need code to return the default value.
Instead of -dequeueReusableCellWithIdentifier:, use -dequeueReusableCellWithIdentifier:forIndexPath:. Then you won't need to follow it with a test to see if a cell was returned (it always will be). You'll need to register your nib with -registerNib:forCellReuseIdentifier:. Or better yet, just design it in the storyboard.
It appears that your custom table view cell has a UIImageView named backGroundImage. That should be added as a subview to the cell's backgroundView property (which you'll need to create - the view, not the property, which is already part of UITableViewCell). Set the image view's autoresizingMask so it will resize with the backgroundView:
- (void)awakeFromNib
{
[super awakeFromNib];
self.backgroundView = [[UIView alloc] initWithFrame:self.bounds];
self.backGroundImage.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.backGroundImage.frame = self.backgroundView.bounds;
[self.backgroundView addSubview:self.backGroundImage];
}
Remove the -tableView:heightForRowAtIndexPath: method. You only want to use this if you return different values. The default row height is 44.0, so you don't need to do anything else.
Related
I am expanding a UITableView cell on click.When the cell expands I have to load a UIView into it.My problen is that I am able to see the UIView on few occasions and sometimes it doesn't display ? The UIView is to be loaded in each and every expanded cell.
Expansion is done like this:-
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat kExpandedCellHeight =300;
CGFloat normalCellHeight = 94;
if ([self.expandedCells containsObject:indexPath]) {
return kExpandedCellHeight;
}else{
return normalCellHeight;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"Cell";
ListCell *cell =(ListCell*) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:#"ListCell" owner:self options:nil];
cell = nibs[0];
}
cell.Name.text = [[nameArray objectAtIndex:indexPath.row]valueForKey:#"opName"];
if (isExpanded) {
backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 95, 320,205)];
[backgroundView setBackgroundColor:[UIColor colorWithRed:(235/255) green:(235/255) blue:(235/255) alpha:0.1]];
[cell.contentView addSubview:backgroundView];
container = [[UIView alloc]initWithFrame:CGRectMake(40, 67, 240, 120)];
container.backgroundColor = [UIColor whiteColor];
//I am adding buttons to this scrollview after webservice response,once buttons are loaded I am trying to load the above container on the background view.
container_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(20, 5, 220, 110)];
[container_scrollView setBackgroundColor:[UIColor whiteColor]];
[container addSubview:container_scrollView];
}
return cell;
}
Now I do get response from webservice.Buttons are added as well.However I can see the container view loaded sometimes and sometimes it doesn't show.What must be the reason?What is causing this behaviour?
This is how I load the container onto background view.
//After container is loaded with buttons.
if(backgroundView){
[backgroundView addsubView:container];
}
Declaration stuff:
#interface ListViewController ()
{
UIView *backgroundView;//Used in expanded cell.
UIView *container;
BOOL isExpanded; //I set this to NO in viewDidLoad initially.
UIScrollView *container_scrollView;
}
You should consider having two different cells on your storyboard or two xib files, each with its correspoding cellidentifier. One will be your normal cell and the other the expanded cell. Then whenever a user taps on one normal cell you should 'replace'(redraw) that specific cell with your second type cell (an expanded cell).
UPDATE
For performing the update of the cells you could use any of these methods. more here
reloadData
reloadRowsAtIndexPaths:withRowAnimation:
reloadSections:withRowAnimation:
Those will cause that some of your Table View Data Source delegate methods get called again, you should then use some logic to decide which kind of cell to draw. A quick draft:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier;
// Here you should implement your custom logic to check if you want a basic or expanded cell.
if (IWantBasicCell) {
cellIdentifier = #"basicCell";
} else {
cellIdentifier = #"expandedCell"
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
[cell myCustomLoadInformationMethod:myCustomData];
return cell;
}
The Storyboard: http://s7.directupload.net/images/140717/z5hwmezv.png
Hey guys,
Ive got an app that recursively triggers a the same tableview controller (lets say there are files and folders in it) until you trigger a file instead of a folder. When a file is clicked, it jumps into the GLKit View Controller.
Now I want to resize the tableView programmatically, which wont work. I already got the window size, which I'm going to use for calculation the position and size of the tableView:
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
I tried different ways to change the size like the following, which dont change anything.
mainTableView.frame = CGRectMake(0, 0, screenWidth, screenHeight);
It works If I programmatically create the mainTableView, but then my segue gets deleted and I did not found any solution to create a segue programmatically.
It would be great if you can help me to find a solution that works with a storyboard tableView.
Step 1: Add delegate UITableViewDataSource,UITableViewDelegate
#interface viewController: UIViewController<UITableViewDataSource,UITableViewDelegate>
{
UITableView *tableView;
}
Step 2:
-(void)viewDidLoad
{
tableView=[[UITableView alloc]init];
tableView.frame = CGRectMake(10,30,320,400);
tableView.dataSource=self;
tableView.delegate=self;
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
[tableView reloadData];
[self.view addSubview:tableView];
}
Step 3: Properties for tableview
//-- For table sections
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
//-- For no of rows in table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//-- Table header height if needed
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}
//-- Assign data to cells
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath] ;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text=[your_array objectAtIndex:indexPath.row]; ***(or)*** cell.textLabel.text = #"Hello";
return cell;
}
//-- Operation when touch cells
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Your custom operation
}
Looking at your pic makes me think your CustomerListVC is a UITableView subclass which means the tableview is the root view and if you are using a UINavigationController based flow then you can't resize the root view readily.
What you can do is place the UITableView in a container and manipulate its constraints from the controller.
first change
class CustomerListVC : UITableViewController
to
class CustomerListVC : UIViewController
next throw away the Customer List instance in Interface Builder and drag in a new UIViewController instance instead. Xcode doesn't like you changing the base class of its stock objects.
make the new UIViewController instance a CustomerListVC type FileOwner and drag a UITableView into the content view.
Set edge constraints and add outlets to the constraints in your view controller.
From there play with the table as you see fit. e.g
-(void)squishAtBottomLeftAnimated:(BOOL)animate {
CGFloat animateTime = animate? 0.5:0;
[UIView animateWithDuration:animateTime animations:^{
self.topEdgeConstraint.constant = 400;
self.bottomEdgeConstraint.constant = 5;
self.leadingEdgeConstraint.constant = 5;
self.trailingEdgeConstraint.constant = 200;
[self.view layoutIfNeeded];
}];
}
I created several cells with Interface Builder, and I'm using them to fill a UITableView. In other words, I have 3 classes for 3 different kinds of cell, and an other view which contains a UITableView.
- My UITableView containing different kinds of cells :
Here's my problem :
On the iPhone emulator, it looks great. But on the iPad emulator, the custom cells width is fixed. The UITableView width fits to the screen width, so it's good, but the UITableViewCells does not fit to the UITableView. I want to force the custom UITableViewCells to take the UITableView width.
Is there anything to do in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathmethod, where I instanciate my custom cells ?
Or do I have to write a thing like self.fitToParent; in the custom cells header file ?
EDIT (schema) :
EDIT 2 (cellForRowAtIndexPath method) :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifierType1 = #"cellType1";
static NSString *cellIdentifierType2 = #"cellType2";
NSString *currentObjectId = [[myTab objectAtIndex:indexPath.row] type];
// Cell type 1
if ([currentObjectId isEqualToString:type1])
{
CelluleType1 *celluleType1 = (CelluleType1 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifierType1];
if(celluleType1 == nil)
celluleType1 = [[CelluleType1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierType1];
celluleType1.lblAuteur.text = #"Type1";
return celluleType1;
}
// Cell type 2
else if ([currentObjectId isEqualToString:type2])
{
CelluleType2 *celluleType2 = (CelluleType2 *)[tableViewdequeueReusableCellWithIdentifier:cellIdentifierType2];
if(celluleType2 == nil)
celluleType2 = [[CelluleType2 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierType2];
celluleType2.lblAuteur.text = #"Type2";
return celluleType2;
}
else
return nil;
}
}
I think uitableviewcell's width is the same as the tableview's width.You can try to set cell's background color to test it. cell.backgroundColor = [UIColor redColor] ;
You should create a class which inherit from UITableViewCell and override it's method - (void)layoutSubviews , adjust your content's frame there.
I resolved my problem using the following code in each custom cell class. It's not very clean, but I can't spend one more day on this issue...
- (void)layoutSubviews
{
CGRect contentViewFrame = self.contentView.frame;
contentViewFrame.size.width = myTableView.bounds.size.width;
self.contentView.frame = contentViewFrame;
}
Thank you for your help KudoCC.
- (void)awakeFromNib {
[super awakeFromNib];
// anything you write in this section is taken with respect to default frame of width 320.
}
awakeFromNib is called when [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; is processed- anything you write in section is taken with respect to default frame of width 320.
You need to make another custom function and call it after cell gets initialized.
For eg:-
#implementation CheckinTableViewCell{
UILabel *NameLabel;
UILabel *rollLabel;
}
- (void)awakeFromNib {
[super awakeFromNib];
NameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
rollLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self.contentView addSubview:NameLabel];
[self.contentView addSubview:rollLabel];
}
-(void) bindView{
NameLabel.frame = CGRectMake(10, 10, self.contentView.frame.size.width-20, 20);
rollLabel.frame = CGRectMake(10, 30, NameLabel.frame.size.width, 20);
}
and call this function in tableview cellForRowAtIndex:-
-(UITableViewCell*) tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
CheckinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell ==nil){
cell = [[CheckinTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.name = #"Harry";
cell.rollno = #"123456";
[cell bindView];
return cell;
}
I want to accomplish something like this :
see there's only one data but, background color continue until end.
I understand I can do inside tableview delegate of tableView:willDisplayCell:forRowAtIndexPath:. but then it doesn't go to empty cell, hence my empty cell always be white.
I used the following code to display cell alternative color even if cell is not initialized.I have done this work on scrollViewDidScroll as showing below:--
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
UIView *view=[[UIView alloc] initWithFrame:tblView.frame];
view.backgroundColor=[UIColor greenColor];
UIView *cellView;
int y=0;
int i=0;
for (UIView *view in tblView.subviews) {
if ([NSStringFromClass([view class]) isEqualToString:#"_UITableViewSeparatorView"]) {
cellView=[[UIView alloc] initWithFrame:CGRectMake(0, y, 320, 44)];
if (i%2==0) {
cellView.backgroundColor=[UIColor redColor];
}else{
cellView.backgroundColor=[UIColor greenColor];
}
[view addSubview:cellView];
i++;
}
}
tblView.backgroundView=view;
}
And got the correct result on scrolling table view. But the problem is it works when user scrolls the tableView atleast once a time.
If you will get success to fire event on tableView completes its reloading.Then it will be fine.
Here is output I got on scrolling tableView.
I also write this method to call didScrollMethod manually but doesn't seems to work perfectly.
[tblView.delegate scrollViewDidScroll:(UIScrollView*)tblView.superclass];
But calling method like code below absolutely works fine.
- (void)viewDidLoad
{
tblView=[[MyFirstView alloc] init];
tblView.delegate=self;
[tblView setFrame:self.view.frame];
[self.view addSubview:tblView];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewDidAppear:(BOOL)animated{
[tblView.delegate scrollViewDidScroll:(UIScrollView*)tblView.superclass];
}
Means after loading tableView in viewDidLoad call didScroll in viewDidAppear works fine.
Insert below code if fluctuates first row while scrolling.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view=[[UIView alloc] init];
return view;
}
You have to set the backgroundColor to the contentView of a UITableViewCell.
Sample as below:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"identifier"];
if (cell==nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"identifier"]autorelease];
cell.contentView.backgroundColor= [UIColor greenColor];
}
return cell;
}
To have alternate colors in your cells of tableView, you can do the following;
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"identifier"];
if (cell==nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"identifier"]autorelease];
}
if(indexPath.row % 2)
{
cell.contentView.backgroundColor= [UIColor greenColor];
}
else
{
cell.contentView.backgroundColor= [UIColor yellowColor];
}
return cell;
}
A table with plain style doesn't show rows below the last row so there is no way to produce the desired effect using table view cells. About your only option would be to create a view with the alternating pattern and make the view the table view's footer view.
This view would need to deal with being updated as the number of actual rows in the table changes to/from odd and even. And you need to make it tall enough so if the user scrolls the table up a bunch, the footer still reaches the bottom of the screen.
You can setup some placeholder cells in addition to your cell with 'Monthly meeting', something like:
return amount of rows as 1 + (rows to fill screen) in the tableView:numberOfRowsInSection:
In the tableView:cellForRowAtIndexPath: - check for index path of the cell, if its row = 0, then this is your action cell, otherwise, update cells background, do the same in the tableView:willDisplayCell:forRowAtIndexPath:. Make sure to remove selectionStyle for your placeholder cells.
Or, you can use 2 cells - first one - again, your 'Monthly meeting' cell, and second one - a cell with height enough to cover screen from first cell to the bottom with image of striped cells.
This is simple to do. Just have as many items in your data source array as you want to see rows, and have all but the first one be empty strings. In willDisplayCell:forRowAtIndexPath: apply a background color to all the odd numbered cells.
- (void)viewDidLoad {
[super viewDidLoad];
self.theData = #[#"Monthly Meeting",#"",#"",#"",#"",#"",#"",#"",#"",#""];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.theData.count;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 2 == 1) {
cell.backgroundColor = [UIColor colorWithRed:232/255.0 green:238/255.0 blue:222/255.0 alpha:1];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = self.theData[indexPath.row];
return cell;
}
Set backgroundColor to the contentView of a UITableViewCell with the help of simple mathematics, Example:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"identifier"];
if (cell==nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"identifier"]autorelease];
if (i%2==0) {
cell.contentView.backgroundColor= [UIColor greenColor];
}else{
cell.contentView.backgroundColor= [UIColor redColor];
}
}
return cell;
}
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.