how to customize the tableview has shown in figure - objective-c

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];

Related

UITableViewCell not loading label on first time properly

My TableViewCell is not loading content in first time after I scrolling up the table it will shows the full view.I have five sections in the tableView only (section =1) got this problem.
I have attached my codes here.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[super tableView:tableView cellForRowAtIndexPath:indexPath];
return cell;
}
On load view
After Scroll up
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
return 10;
}
else if (section == 1)
{
return 10;
}
else
{
return 0;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
{
if(section == 0)
{
return 40;
}
else if(section == 1)
{
return 40;
}
else
{
return 0;
}
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *View=[[UIView alloc]initWithFrame:CGRectMake(0,0,768,40)];
View.backgroundColor=[UIColor colorWithRed:211.0/255.0 green:211.0/255.0 blue:211.0/255.0 alpha:1.0];
UILabel *lbl_title=[[UILabel alloc]initWithFrame:CGRectMake(10,0,700,40)];
lbl_title.backgroundColor=[UIColor clearColor];
lbl_title.font= [UIFont fontWithName:#"ArialMT" size:20];
lbl_title.textColor =[UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];
if (section == 0)
{
lbl_title.text=#"Used";
}
else if (section == 1)
{
lbl_title.text=#"Expiring";
}
[View addSubview:lbl_title];
[lbl_title release];
return View;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"cell %d",indexPath.section];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if (indexPath.section == 0)
{
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(180,10, 360, 60);
lbl.backgroundColor=[UIColor clearColor];
lbl.textColor = [UIColor blackColor];
lbl.numberOfLines = 3;
lbl.font = [UIFont fontWithName:#"ArialMT" size:18];
lbl.text = #"";
[cell.contentView addSubview:lbl];
[lbl release];
}
else if (indexPath.section == 1)
{
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(180,10, 360, 60);
lbl.backgroundColor=[UIColor clearColor];
lbl.textColor = [UIColor blackColor];
lbl.numberOfLines = 3;
lbl.font = [UIFont fontWithName:#"ArialMT" size:18];;
lbl.text = #"";
[cell.contentView addSubview:lbl];
[lbl release];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = arrayValues[indexPath.row];
return cell;
}

UISearchDisplayController Not Displaying Results in searchResultsTableView

I have a UISearchDisplayController properly connected to the header of a custom UITableView in IB. However, when I search for anything the searchResultsTableView only displays "No Results", and I cannot seem to find where the code is incorrect.
searchResults Property
- (NSMutableArray *)searchResults {
if (!_searchResults) {
_searchResults = [[NSMutableArray alloc] initWithCapacity:self.listingPreviews.count];
}
return _searchResults;
}
Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSInteger numberOfRows = 0;
if (tableView == self.tableView) {
numberOfRows = self.listingPreviews.count;
} else {
numberOfRows = self.searchResults.count;
}
return numberOfRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Listing";
ListingPreviewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[ListingPreviewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
if (tableView == self.tableView) {
cell.listingPreview = [self.listingPreviews objectAtIndex:indexPath.row];
} else {
NSLog([[self.searchResults objectAtIndex:indexPath.row] title]);
cell.listingPreview = [self.searchResults objectAtIndex:indexPath.row];
}
return cell;
}
Search Bar Delegate Method
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSInteger searchTextLength = searchText.length;
for (ListingPreview *listingPreview in self.listingPreviews) {
if ((listingPreview.title.length >= searchTextLength) && ([listingPreview.title rangeOfString:searchText].location != NSNotFound)) {
[self.searchResults addObject:listingPreview];
}
}
}
Just try this instead:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Listing";
ListingPreviewTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell.
if (tableView == self.tableView) {
cell.listingPreview = [self.listingPreviews objectAtIndex:indexPath.row];
} else {
NSLog([[self.searchResults objectAtIndex:indexPath.row] title]);
cell.listingPreview = [self.searchResults objectAtIndex:indexPath.row];
}
return cell;
}
Please note that the change was applied on:
ListingPreviewTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
This way you deque a Cell from your own tableView and not from the one created by UISearchDisplayController.
It seems that nothing in your code forces searchDisplayController to reload it's searchResultTableView.
The standard approach is to set UISearcDisplayController's delegate (note the protocol - UISearchDisplayDelegate) and implement – searchDisplayController:shouldReloadTableForSearchString:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
// perform your search here and update self.searchResults
NSInteger searchStringLength = searchString.length;
for (ListingPreview *listingPreview in self.listingPreviews) {
if ((listingPreview.title.length >= searchStringLength) && ([listingPreview.title rangeOfString:searchString].location != NSNotFound)) {
[self.searchResults addObject:listingPreview];
}
}
// returning YES will force searchResultController to reload search results table view
return YES;
}

Displaying two sections of specific size in a table view

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.

use 2 tableview

Hi all freind, please how can I use 2 TableView ,i use this code but my problem is I have the same name for cellule
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (choix==1) {
NSLog(#"%d",choix);
return [mutable3 count];
}
else {
NSLog(#"%d",choix);
return [mutable2 count];
}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (choix==1) {
NSLog(#"cc%d",choix);
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [mutable3 objectAtIndex:indexPath.row];
// Configure the cell...
return cell;
}
else {
NSLog(#"vvv%d",choix);
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [mutable2 objectAtIndex:indexPath.row];
// Configure the cell...
return cell;
}
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (choix==1) {
NSLog(#"%d",choix);
langue.text =[mutable3 objectAtIndex:indexPath.row];
langView.hidden=TRUE;
}
else {
NSLog(#"%d",choix);
compain.text =[mutable2 objectAtIndex:indexPath.row];
langView.hidden=TRUE;
}
}
Ok its going to be a simple explanation, hope you understand. If you dont please ask and I will elaborate.
TableView Decleration Example:
UITableView tableView1;
UITableView tableView2;
Each tableview has a variable name which it links to that differs. Because we are working with pointers it allows us to do the following in the delegate methods(All of them, this is only one example)
Delegate Method Example:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView== tableView1)
{
return 2;
}
else if(tableView== tableView2) //this if statement is not really needed since if you only have 2 table views the second will automatically fall into the else
{
return 3;
}
else //This else is only needed if you use the second if statement
return 0;
}
And you can use the same approach for all delagate methods

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;
}