Add a subview in a UITableViewController - iphone

I am using a UITableViewController, I have resized the height of the table view and moved it a little down. Now I'd like to add a UIImageView above that table view but the only thing that I can do is add that UIImageView as a subview of the UITtableView of the UITableViewController, so that UIImageView is not above the UITableView and the image scrolls with the table view.
How can I accomplish what I'm trying ? Here is the code I've been using :
UIImageView *tabImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"temp.png"]];
[self.view addSubview:tabImageView];
Thanks!

I believe UITableViewController requires that its view be a UITableView.
In the past, I've worked around this by changing my view controller from subclassing UITableViewController to subclassing UIViewController and implementing the UITableViewDataSource and UITableViewDelegate protocols. Then you can have your view be whatever you want, including shifting your UITableView down and adding a UIImageView.
Not sure if this is still the case in 4.2, though.

EDIT (December 16, 2013)
Just want to clarify that this is really not a great idea for many reasons, a bunch of which are listed in the comments. You should really be doing what Shaggy Frog is suggesting, or using View Controller Containment.
Original Answer
You want the UIImageView to be static at the top of the tableView? If that's so, you need to set it as a subview of the UINavigationController, not the UITableView. Easiest way to do this is with [self.view.superview addSubview:tabImageView]. (although as Shaggy Frog said, it's probably best to change from a UITableViewController to a plain UIViewController, and create the UITableView manually)
If you want the UIImageView to be attached to the UITableView, so that when you scroll down, it disappears, you can just set it as the table header with [self.tableView setTableHeaderView:tabImageView]

Take 4 cells in your static UITableView in UITableViewController with height of 44 PX & width of 320PX. Comment all tableView Delegate methods in UITableViewController.m file and put this code.
- (void)viewDidAppear:(BOOL)animated
{
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 170)];
backgroundView.image=[UIImage imageNamed:#"default-cover-image.jpg"];
[self.view.superview addSubview:backgroundView];
[self.tableView setFrame:CGRectMake(0, 170, 320, 960)];
[[[UIApplication sharedApplication] keyWindow] setBackgroundColor:[UIColor whiteColor]];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
if ((indexPath.row)==0)
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"one"];
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
backgroundView.image=[UIImage imageNamed:#"sectionHeader.png"];
[cell.contentView addSubview:backgroundView];
}
if ((indexPath.row)==1)
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"two"];
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
backgroundView.image=[UIImage imageNamed:#"sectionHeader2.png"];
[cell.contentView addSubview:backgroundView];
}
if ((indexPath.row)==2)
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"three"];
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
backgroundView.image=[UIImage imageNamed:#"sectionHeader3.png"];
[cell.contentView addSubview:backgroundView];
}
if ((indexPath.row)==3)
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"four"];
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
backgroundView.image=[UIImage imageNamed:#"sectionHeader.png"];
[cell.contentView addSubview:backgroundView];
}
return cell;
}
Hope, It'll work.

Related

How can I create an accessory view for my table cell like this?

Basically I need two labels. one with a number and other with the constant string letters on next line.Also I need a speech bubble as background for accessory view.I know how to create one label , but no idea about setting two label as accessory view. Please help
You can create a UIView and add all the necessary views to it as its subviews, then make that single view the accessory view of the table view cell:
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
[container addSubview:backgroundImage];
[container addSubview:firstLabel];
[container addSubview:secondLabel];
cell.accessoryView = container;
For that, you need to create one ImageView or UIView and set the image accordingly.
Then you have set the accessory view of the cell like below:
cell.accessoryView = <your view>;
Hope, you got an idea.
Cheers!
Its works fine for me.....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(25, 25, 100, 21)];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(25,42, 100, 21)];
label1.backgroundColor = [UIColor clearColor];
label2.backgroundColor = [UIColor clearColor];
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 110)];
[container setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"Bukkl.png"]]];
[container addSubview:label1];
[container addSubview:label2];
label1.text = #"HIIIIIIII";
label2.text = #"###BBBBBB";
cell.accessoryView = container;
}
return cell;
}
If it works well than don't forget to upvote..

Cell image only appearing after scrolling

I have a UITableView with method cellForRowAtIndexPath shown below, the problem is that cellImage only appears after having scrolled completely up (so no rows are visible) and then releasing. The strange thing is that I have identical code in another class for a separate table view and the image appears fine. Any ideas?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
}
[cell.textLabel setText:[items objectAtIndex:indexPath.row]];
[cell.textLabel setFont:[UIFont fontWithName:#"Helvetica" size:15.0f]];
[cell.textLabel setTextColor:[UIColor whiteColor]];
[cell setBackgroundColor:[[UIColor alloc] initWithRed:117.0/255.0 green:118.0/255.0 blue:121.0/255.0 alpha:1.0]];
[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundView:[[UIView alloc] init]];
[self.view setBackgroundColor:[[UIColor alloc] initWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]];
UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"next#2x.png"]];
[cell addSubview:cellImage];
[cellImage setFrame:CGRectMake(0, 0, 26, 24)];
[cellImage setCenter:CGPointMake(cell.frame.size.width - 30, cell.frame.size.height/2)];
return cell;
}
[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundView:[[UIView alloc] init]];
The lines dont belong to the method. You call the methods every time you scroll for every cell. Put them in your viewDidLoad method, after you have created your table view.
UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"next#2x.png"]];
[cell addSubview:cellImage];
These lines are really very BAD and may cause a memory leak.
Everytime you scroll the table view you add a imageview to the cell. Table view reuses the cells. It means when cell is reused, you add a new imageview to the cell. After you scroll more times you have x times imageview on every cell, unless you remove them somewhere in you code.
Other point is, if you want to add some subview to a cell (eg. in init method of the cell) then add it to contenview of the cell like this
[cell.contentView addSubview:someView];
Otherwise your subview may overlap accessory views. And you can have problems when your cell is resized.
I advise you read the guide from the following link:
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW1
As captured in many of the comments, there are a number of issues:
Set your view and tableView background in your viewDidLoad method (or via your XIB), not in cellForRowAtIndexPath
Use the cell.imageView property to set a left icon, OR, alternatively, to build a custom cell you would use [cell.contentView addSubview:cellImage]. If you do the latter, you should also add your textLabel in the same manner as a UILabel.
To set the background of a tableview cell, use this delegate method:
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[[UIColor alloc] initWithRed:117.0/255.0 green:118.0/255.0 blue:121.0/255.0 alpha:1.0]];
}
though for efficiency you should instantiate this UIColor once in your viewcontroller and set it as [cell setBackgroundColor:self.cellBackground];
The problem you are facing is due to reuseIdentifier for cell.
Also you are creating Imageview everytime cellForRowAtIndexPath is executed.
i will suggest two solution here for you.
1st Option :- just replace this code :
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.accessoryType = UITableViewCellAccessoryNone;
}
this is not proper way when you are dealing with dynamic images, means you are downloading thumbnail from server and data content is more, if it's small and static data then it's fine for you.
2nd Option :- just use this formatted code this will not create imageview everytime just use object from current cell with tag.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
UIImageView *cellImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 26, 24)];
cellImage.tag = 88;
[cell addSubview:cellImage];
[cellImage setCenter:CGPointMake(cell.frame.size.width - 30, cell.frame.size.height/2)];
}
[cell.textLabel setText:[items objectAtIndex:indexPath.row]];
[cell.textLabel setFont:[UIFont fontWithName:#"Helvetica" size:15.0f]];
[cell.textLabel setTextColor:[UIColor whiteColor]];
[cell setBackgroundColor:[[UIColor alloc] initWithRed:117.0/255.0 green:118.0/255.0 blue:121.0/255.0 alpha:1.0]];
[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundView:[[UIView alloc] init]];
[self.view setBackgroundColor:[[UIColor alloc] initWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]];
UIImageView *cellImage = (UIImageView *)[cell viewWithtag:88];
cellImage.image = [UIImage imageNamed:#"next#2x.png"];
}
Fixed using the following code:
UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"next#2x.png"]];
[cell setAccessoryView:cellImage];

How to set a background image in a static UITableViewCell?

I tried the following code, but the cell doesn't change at all:
- (void)viewDidLoad
{
[super viewDidLoad];
//Set background image of cells
UITableViewCell *cell;
cell = [CalculatorTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Spots.png"]];
}
Two things. First of all, cells should be created in the cellForRowAtIndexPath method. Secondly, you should make your background view and then add it to the cell content view.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell = [CalculatorTableView alloc] initWithStyle:UITableViewStyleDefault reuseIdentifier:nil]; //This wil obviously depend on your subclass
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Spots.png"]];
[cell.contentView addSubview:backgroundView];
}
cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"Spots.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
That might be a better way to do it.
OR, you can try this:
You can try this code that will help you.
Put this code in cellForRowAtIndexPath method
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 277, 58)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
av.image = [UIImage imageNamed:#"Spots.png"];
cell.backgroundView = av;
You put your cell initializing code in viewDidLoad? That is very wrong, it should be in:
cellForRowAtIndexPath:
You might want to check out this link:
TableView programming
Cells created using static table views don't exist until after viewWillAppear: has been called. Move your code above into that method (and make sure you call the super implementation first.

Problem in customize cell selection Style in UITableView in iPhone

In my application ,I am using Grouped style for TableView. In that I want to customize the cell selection Style.I want that selection Style to be red.
I am using the below code for that:
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor redColor];
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];
By using the above code. I have a problem.Since I have taken grouped Style table , in the
selection of first and last rows, the selection is appear with sharp edged rectangle instead of
appeaing round Corners.
Can Anyone Help me in this.
Thanks in Advance.
Try this ,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SelectedCellBackground.png"]] autorelease];
}
// Cell configuration takes place here
}
add QuartzCore framework. and import QuartzCore/QuartzCore.h framework in .m file. and after that add following code in cellForRowAtIndexPath Method.
UIImageView *imv = [[UIImageView alloc]init];
imv.backgroundColor=[UIColor redColor];
[cell setSelectedBackgroundView:imv];
CALayer *layer = imv.layer;
[layer setMasksToBounds:YES];
[layer setCornerRadius:10.0];
[imv release];
Override -(void)setSelected:animated: in your subclass.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if (selected)
[self setBackgroundColor:[UIColor redColor]];
else
[self setBackgroundColor:[UIColor whiteColor]];
}
You can add fancy animation here to blend in and fade out the background upon selection and deselection if animated is YES.
In general UITableViewCell can be a bit awkward to subclass, be patient here.
UIImageView *imageVieww=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell2.bounds.size.width, cell2.bounds.size.height)];
imageVieww.image=[UIImage imageNamed:#"mavilik.png"];
[cell2 setSelectedBackgroundView:imageVieww];
[imageVieww release];

To display Headerview with image and labels in the viewcontroller

I am new to iPhone development. I have created one view controller and I used grouped table view. Now I want to display the header view in my viewcontroller with image and labels. Please guide me and help me out in this problem.
Thanks.
Do you mean a headerView or a sectionHeaderView? You can add subviews to the headerView in the viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 225)];
label.text = #"BlaBla";
[self.tableHeaderView addSubview:label];
}
You specify size and position of the label with the initWithFrame method, and add the label as subview to the tableHeaderView - you can do this with multiple labels.
If you mean the sectionHeader you have to implement the tableView:viewForHeaderInSection: method, where you have to create a new view, and add different subviews to it:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 40)];
label.text = #"BlaBla";
[view addSubview:label];
[label release];
return [view autorelease];
}
In this case you also have to implement the method tableView:heightForHeaderInSection: which has to return the height of the view you create in the above method:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 50.0f;
}