Displaying two sections of specific size in a table view - objective-c

I am trying to fill a table view with an array, but I want two sections, and in the first section I want to display the imformation from indexpath.row == 0 to indexpath.row == 4 and in the second section the information from indexpath.row == 5 to indexpath.row == 9. How can I do that? I have this code for the second section:
if (indexPath.section == 1){
static NSString *CellIdentifier = #"LazyTableCell";
static NSString *PlaceholderCellIdentifier = #"PlaceholderCell";
int nodeCount = [self.entries count];
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
if (nodeCount > 0)
{
if (indexPath.row > 4) {
AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];
cell.textLabel.text = appRecord.artist;
cell.detailTextLabel.text = appRecord.appName;
if (!appRecord.appIcon)
{
if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
{
[self startIconDownload:appRecord forIndexPath:indexPath];
}
cell.imageView.image = [UIImage imageNamed:#"Placeholder.png"];
}
else
{
UIImage *image = appRecord.appIcon;
CGSize itemSize = CGSizeMake(83.0, 48.0);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, 83.0, 48.0);
[image drawInRect:imageRect];
appRecord.appIcon = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = appRecord.appIcon;
}
}
return cell;
}
}

set the number of section in this function
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
Perhaps you also want to implement
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return #"section title";
}
set the number of lines in the following function
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5; // since you have 0-4 and 5-9 for the two sections
}
after seeing in your code [self.entries objectAtIndex:indexPath.row];
I assumed self.entries is the one array that you have been talking about.
You can initialize the UITableViewCell property in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
by using a combination of section and row to get the index of your self.entries, probably something like
AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row+5*indexPath.section];
I hope i got your question right.

Related

add background image for sections in UITableView

is it possible to change background image of each sections in uitableview?
I want to add background image for each sections in uitableview
does anyone know how can I do that?
Thanks in advance!
like this picture --> put different background images for wednesday , Thursday and friday separately
Edit I want to add image 1 for wednesday image 2 for Thursday image 3 for friday and .....
how can I specify that ?
Edit
this the code for creating sections header I want to have background also
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if(section == 0)
return #"Monday";
else if(section == 1){
return #"Tuesday";
}else if(section == 2){
return #"Wednesday";
} else if(section == 3){
return #"Thuesday";
} else if(section == 4){
return #"Friday";
} else if(section == 5){
return #"Saturday";
}else
return #"Sunday";
}
You could change the background in the cellForRowAtIndexPath delegate method based on the indexPath, like so:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TaskCellRow";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
int maxRow = 3;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: [NSString stringWithFormat: #"background_image%i.png", MIN(indexPath.row, maxRow)]]];
}
else
{
UIImageView *background = (UIImageView *)cell.backgroundView;
background.image = [UIImage imageNamed: [NSString stringWithFormat: #"background_image%i.png", MIN(indexPath.row, maxRow)]];
}
return cell;
}
// To change header backgrounds
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
int maxRow = 3;
UIImageView *headerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: [NSString stringWithFormat: #"background_image%i.png", MIN(section, maxRow)]]];
return headerView;
}
You would then just create images, numbered for the desired amount header/rows, ie. background_image0.png, background_image1.png, background_image2.png, ... and so forth. The MIN will cap the amount off at the whatever you decide is the max backgrounds. Hope that helps.
EDIT: Changed cellForRowAtIndexPath based on Henri's comments. I overlooked that, thanks! This is for ARC compatibility.
You can use:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
to specify any kind of UIView for a section header. This other delegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
lets you specify the height.
Just alloc/init the UIView in the first one using the table's width and the height from the second method and then add any number of views to it, such as a UIImageView for a background then a label for the title.
Iterating (and mostly correcting) example given by ezekielDFM. Note, this code is not ARC compatible (which previous example may have been).
// To change cell backgrounds
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"TaskCellRow";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
int maxRow = 3;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIImageView *imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: [NSString stringWithFormat: #"background_image%i.png", MIN(indexPath.row, maxRow)]]] autorelease];
cell.backgroundView = imageView;
} else {
// Reusing, we need to swap out the image of the background
cell.backgroundView.image = [UIImage imageNamed: [NSString stringWithFormat: #"background_image%i.png", MIN(indexPath.row, maxRow)]];
}
return cell;
}
// To change header backgrounds
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
int maxRow = 3;
UIImageView *headerView = [[[UIImageView alloc] initWithImage:[NSString stringWithFormat:#"background_image%i.png", MIN(section, maxRow)]] autorelease];
return headerView;
}
As i guess it is not possible to change the background image for Table view Sections. If you want to do such a requirement, please try with cell for row as Sections. i.e. for each row treat as section of table & in that add 1 more table view with different tag. It will full your req. or else for each row take

UITableView duplicate row's

This is the code im using in the 'cellForRowAtIndexPath'
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* cellIdentifier = #"OBBirthControlMethodsTableCell";
OBCustomDetailCell *cell = (OBCustomDetailCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
NSLog(#"cell id - %#",cell.subviews);
CGRect frame = [tableView rectForRowAtIndexPath:0];
if(nil == cell)
{
cell = [[[OBCustomDetailCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
if (indexPath.row != 3)
{
//Setting the basic template
UIView *template = [[UIView alloc] init];
template.tag = indexPath.row+10;
NSLog(#"path index = %d",indexPath.row);
UIImageView *templateImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,
200,
frame.size.height)];
UILabel *templateLabel = [[UILabel alloc] initWithFrame:CGRectMake(templateImage.frame.size.width+20,
0,
cell.frame.size.width - templateImage.frame.size.width+20,
frame.size.height)];
[template addSubview:templateImage];
[template addSubview:templateLabel];
[cell.contentView addSubview:template];
}
}
UIView *templateView = [cell.contentView viewWithTag:indexPath.row + 10];
if (templateView)
{
NSLog(#"Gotten a templateView object");
if (indexPath.row == 0)
{
templateView.frame = frame;
for (UIView *view in templateView.subviews)
{
if ([view isKindOfClass:[UIImageView class]])
{
[(UIImageView *)view setImage:[UIImage imageNamed:#"baby.jpeg"]];
}
else if ([view isKindOfClass:[UILabel class]])
{
[(UILabel *)view setText:#"This is not working"];
}
}
}
else
{
templateView.frame = CGRectMake(0, 50,
frame.size.width,
frame.size.height);
}
}
return cell;
}
But the issue is that the new cell is giving me the same values os the old cell the new cell that is dequeued once you scroll down ..
EDIT
A duplicate cell is being created as soon as we scroll down to a new cell with the same vales of the 1st cell ..
I would like the UIView to be created for only select rows() ..if (indexPath.row != 3)
and i would like the location of the UIView to be different in some of the rows .. if (indexPath.row == 0)
There are a couple of problems with this bit of code. First and foremost, the primary cause of your issues is this bit:
template.tag = indexPath.row+10;
Why are you doing this? Just use a constant value, like 10. No need to involve the index path, since that will change for each cell. This will cause viewWithTag: to fail for reused cells, and it will return nil.
Second, you can't only set up your template cell for indexPath.row != 3, because at some point, the non-template cell may be reused. It will not have the template views, so the following layout will fail. You'll need to use two reuse identifiers for the two types of cells. The final product should look something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *templateCellIdentifier = #"OBBirthControlMethodsTableCell";
static NSString *otherCellIdentifier = #"OtherTableCell";
if (indexPath.row != 3) {
// Handle normal cells
OBCustomDetailCell *cell = (OBCustomDetailCell *)[tableView dequeueReusableCellWithIdentifier:templateCellIdentifier];
if (cell == nil) {
cell = [[[OBCustomDetailCell alloc] initWithStyle:UITableViewStyleDefault reuseIdentifier:templateCellIdentifier] autorelease];
// Set up template cell
}
// Handle per-cell data
} else {
// Handle special cells
OBCustomDetailCell *cell = (OBCustomDetailCell *)[tableView dequeueReusableCellWithIdentifier:otherCellIdentifier];
if (cell == nil) {
cell = [[[OBCustomDetailCell alloc] initWithStyle:UITableViewStyleDefault reuseIdentifier:otherCellIdentifier] autorelease];
// Set up other cell
}
// Handle per-cell data (not really necessary if there's only one of these)
}
}

how to customize the tableview has shown in figure

How to create the tableview has shown below picture
.
If i created section i am missing th boarder line what to do ?
You need to set the UITableView to Grouped Style. Then you need 2 Sections, with no Header.
also you have to set the
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
Example Code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if(section == 0)
return 1;
else
return 2;
}
- (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] autorelease];
}
// Configure the cell...
switch(indexPath.section)
{
case 0:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"Create a Holiday Card";
break;
case 1:
switch(indexPath.row)
{
case 0:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"Login to Photo";
break;
case 1:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"Create a Free Account";
break;
}
}
return cell;
}
Per example above
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;// Amount of sections
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0){
return 1;
}
else if (section == 1){
return 2;
}
}
- (void)tableViewUITableView *)aTableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
I assume you are referring to the STYLE of that example.
[self.view setBackgroundColor: [UIColor blueColor]];
/* also assuming your table sits on a UIViewController */
[myTableView setBackgroundColor: [UIColor clearColor]];
[myTableView setOpaque: NO];

Push a new view when a cell is tapped in Table View

Hey guys,
I have a Table View that has been populated with 21 data:
- (void)viewDidLoad {
self.title = NSLocalizedString(#"Glossary", #"Back");
NSMutableArray *array = [[NSArray alloc] initWithObjects:#"Title", #"Meta Description Tag", #"Meta Keywords", #"Headings", #"Images", #"Frames", #"Flash Contents", #"Charset", #"Favicon", #"W3C Compatibility", #"Page Rank", #"Alexa Rank", #"Indexed Pages", #"Latest Date Cached By Google", #"Backlinks", #"Dmoz Listing", #"Server Info", #"IP", #"Location", #"Server Type", #"Registrar Info", nil];
self.glossaryArray = array;
[array release];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 4;
}
// Category
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
if (section == 0) return #"In-Site SEO";
if (section == 1) return #"Inside Analysis";
if (section == 2) return #"Ranks N Stuff";
if (section == 3) return #"Server Info";
return #"Other";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
if (section == 0) return 7;
if (section == 1) return 3;
if (section == 2) return 6;
if (section == 3) return 5;
return 0;
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
// Configure the cell...
NSUInteger row = [indexPath row];
if ( indexPath.section == 1 ) row += 7;
if ( indexPath.section == 2 ) row += 10;
if ( indexPath.section == 3 ) row += 16;
if ( indexPath.section == 4 ) row += 21;
cell.textLabel.text = [glossaryArray objectAtIndex:row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Now this is the code I used to pussh a new view when a cell is tapped:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger row = [indexPath row];
if (self.glossaryDetailViewController == nil) {
GlossaryDetailViewController *aGlossaryDetail = [[GlossaryDetailViewController alloc] initWithNibName:#"GlossaryDetailViewController" bundle:nil];
self.glossaryDetailViewController = aGlossaryDetail;
[aGlossaryDetail release];
}
glossaryDetailViewController.title = [NSString stringWithFormat:#"%#", [glossaryArray objectAtIndex:row]];
NewReferencemoi_caAppDelegate *delegate = (NewReferencemoi_caAppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.glossaryNavController pushViewController:glossaryDetailViewController animated:YES];
}
This code works perfectly, but the problem is that each and all 21 elements in my table view is opening the one and same nib file that I created. Basically, I want to create 1 UIViewController for each of my 21 elements, where each have their own description of the element, not just using 1 UIViewController for all elements in my Table View, and when each element has been tapped, each open their own view. Apparently, I don't know how to code in that part, so I hope someone can help me out with this part of my iPhone project, thanks
Please do not create 21 different view controllers just to show different items. Instead set a property on GlossaryDetailViewController that holds an instance of your data model item.
Consider this...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = [indexPath row];
if (self.glossaryDetailViewController == nil)
{
GlossaryDetailViewController *aGlossaryDetail = [[GlossaryDetailViewController alloc] initWithNibName:#"GlossaryDetailViewController" bundle:nil];
self.glossaryDetailViewController = aGlossaryDetail;
[aGlossaryDetail release];
}
glossaryDetailViewController.glossaryDetailItem = [glossaryArray objectAtIndex:row];
[self.navigationController pushViewController:self.glossaryDetailViewController animated:YES];
}
Using this approach makes GlossaryDetailViewController responsible for setting it's own data.
Edit
You'll also notice that I removed references to the app delegate. You don't need it to get access to the navigation controller. Each view controller in a navigation controller stack has a reference to it. While I'm thoroughly tearing your code apart, I would also factor out the creation of the view controller by overriding the getter for glossaryDetailViewController, like this:
- (GlossaryDetailViewController *)glossaryDetailViewController
{
if (!glossaryDetailViewController)
{
glossaryDetailViewController = [[GlossaryDetailViewController alloc] init];
}
return glossaryDetailViewController;
}
If you go this route, you can remove the if statement and just call self.glossaryDetailViewController.

How to change the row height for only the last cell in a tableview?

In my table view I had more than 200 rows and Im managing then by keeping loadmoreresults in last cell to display only 40 rows per page.when the user clicks on the load more results 40 more cells is displayed.I changed the row height of loadmoreresults cell as different from other. The problem is when there is no more results the height for other cell is effected with the height of the loadmoreresults cell I dont want to sffect the last row height to other rows.
The code I written as:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
ZohoAppDelegate* appDelegate = (ZohoAppDelegate*) [ [UIApplication sharedApplication] delegate];
int maxResults = [appDelegate.invoiceMaxValues intValue];
int noOfResults = [invoiceArray count] ;
if (noOfResults != 0 )
{
if (maxResults > noOfResults)
{
noOfResults++;
rowCount = noOfResults;
}
}
return noOfResults;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == (rowCount -1))
{
return 50;
}
return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"MasterViewIdentifier"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
UIView* elementView = [[UIView alloc] initWithFrame:CGRectMake(5,5,312,480)];
elementView.tag = 0;
[cell.contentView addSubview:elementView];
[elementView release];
}
UIView* elementView = [cell.contentView viewWithTag:0];
for(UIView* subView in elementView.subviews)
{
[subView removeFromSuperview];
}
UIImageView* imaeView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 78)];
if(indexPath.row == (rowCount-1))
{
elementView.backgroundColor = [UIColor colorWithRed:0.92578125 green:0.92578125 blue:0.92578125 alpha:1];
}
else
{
imaeView.image=[UIImage imageNamed:#"estimatecellbg.png" ];
}
[elementView addSubview:imaeView];
[imaeView release];
return cell;
}
Anyone's help will be much appreciated.
Thank you,
Monish.
Something along the lines of
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == (rowCount -1) && moreResults)
{
return 50;
}
return 80;
}