I have two array. One for Tableview Header and another one for Tableview row. I want to display header wise values for different Rows. Number of sections are depend on that array which is used in Header view.
Number of rows is calculated tableview row array divide by header array.
The UI working perfectly and also values of header and sections are fine but row values appear same for each and every section.
yo have to use this Approach to show different Cell Result
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _mutableArray.count;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(5, 0, tableView.frame.size.width, heightOfSection)];
NSDictionary *dict = [_mutableArray objectAtIndex:section];
NSString *useIt= [dict objectForKey:#"CategoryName"]
return view;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSDictionary *dict = [_mutableArray objectAtIndex:section];
NSArray *array= [dict objectForKey:#"FontNames"];
return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableViewLocal cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// static NSString *cellIdentifier = #"Cell";
UITableViewCell *Cell = [tableViewLocal dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
for (__strong UIView *view in Cell.contentView.subviews) {
[view removeFromSuperview];
view = nil;
}
// [tableViewLocal setAllowsSelection:NO];
/* UILabel *labelDetailName =[[UILabel alloc]init];
labelDetailName.frame =CGRectMake(5, 15, 300, 40);
NSDictionary *dict = [_mutableArray objectAtIndex:(tableViewLocal.tag-100)];
NSArray *arrayLocal = [dict objectForKey:#"FontNames"];
NSString *titleName =[ [arrayLocal objectAtIndex:indexPath.item]objectForKey:#"title"];
labelDetailName.text = titleName;
NSString *fontName = [[arrayLocal objectAtIndex:indexPath.item] objectForKey:#"nameForSystem"];
labelDetailName.font = [UIFont fontWithName:fontName size: kSystemVersion < 7.0 ? 20 : 16];
[Cell.contentView addSubview:labelDetailName]; */
NSDictionary *dict = [_mutableArray objectAtIndex:(tableViewLocal.tag-100)];
NSArray *Array = [dict objectForKey:#"FontNames"];
NSString *localString = [Array objectAtIndex:indexPath.row]; // use it
}
Related
I am making tableview with different sections programmatically. I am using this code Please help me where i am wrong.
I have to show different sections with different cells ,sections of years and cells of movies.
in .h file
{
NSDictionary *movieTitles;
NSArray *years;
}
#property (nonatomic, retain) NSDictionary *movieTitles;
#property (nonatomic, retain) NSArray *years;
in .m file
#synthesize movieTitles;
#synthesize years;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle]pathForResource:#"about" ofType:#"plist"];
NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:path];
movieTitles = dic;
aboutTable = [[UITableView alloc]initWithFrame:CGRectMake(20, 50, self.view.frame.size.width - 40, self.view.frame.size.height) style:UITableViewStyleGrouped];
aboutTable.delegate = self;
aboutTable.dataSource = self;
UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 20, 50, 20)];
[backButton addTarget:self action:#selector(backdb) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[self.view addSubview:backButton];
[self.view addSubview:aboutTable];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [years count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *details = [years objectAtIndex:section];
NSArray *titleSection = [movieTitles objectForKey:details];
return [titleSection count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellidentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier ];
if(!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *details = [years objectAtIndex:[indexPath section]];
NSArray *titleSection = [movieTitles objectForKey:details];
cell.textLabel.text = [titleSection objectAtIndex:[indexPath row]];
return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *details = [years objectAtIndex:section];
return details;
}
You are returning the [years count] for numberOfSectionsInTableView. Where you initialise the years?
And also in viewDidLoad, instead of movieTitles = dic, use the below code:-
movieTitles = [dic copy];
where is your "year" value. It count become Zero. and you have to alloc your value.so first of all add object to array.
e.g.years =[[NSArray alloc]initWithObjects:#"2001",#"2002",#"2003", nil];
Table view don't show beacuse "years is nil" when called
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [years count];
}
as the number of section return 0, so the tableveiw could not add.
resolve this issue.
add this line in viewDidLoad method
years = #[#"2015",#"2015"];
- (void)viewDidLoad {
NSArray *arrYear;
arrYear=[[NSArray alloc]initWithObjects:#"1991",
#"1992",
#"1993",
#"1994",nil]
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdetifier = [NSString stringWithFormat:#"Cell_%#",indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdetifier ];
if(!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdetifier];
cell.textLabel.text = [arrYear objectAtIndex:[indexPath row]];
}
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return years.count;
}
arrYear=[[NSArray alloc]initWithObjects:#"1991",
#"abc",
#"xyz",
#"123",nil]
You have to initialise Your Array values in didload
I am using the following sample code to have the section contents in "Grouped Table View". Showing the contents in "Grouped Table View" is fine.
But, the issue is, it sorts the section content based on alphabetical order, so the order of section header content is not displaying as expected. For example: I want "About" to be shown at the end of section in tableview, but here it is always shows first (because of alphabetical sort does there). How can i display the section content based on the below code without alphabetical sorting. Please advise!
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *arrTemp4 = [[NSArray alloc]initWithObjects:#"Quick Logon",#"Stay Logged On", nil];
NSArray *arrTemp3 = [[NSArray alloc]initWithObjects:#"Notifications",#"Text Messaging",#"Family Members",#"Social Media",nil];
NSArray *arrTemp2 = [[NSArray alloc]initWithObjects:#"Payment Accounts",nil];
NSArray *arrTemp1 = [[NSArray alloc]initWithObjects:#"Phone Nickname",#"Version",nil];
NSDictionary *temp = [[NSDictionary alloc]initWithObjectsAndKeys: arrTemp1,#"About", arrTemp2,#"Payment Preferences", arrTemp3,#"Profile & Preferences", arrTemp4,#"Security ", nil];
self.tableContents = temp;
NSLog(#"table %#",self.tableContents);
NSLog(#"table with Keys %#",[self.tableContents allKeys]);
self.sortedKeys = [[self.tableContents allKeys] sortedArrayUsingSelector:#selector(compare:)];
NSLog(#"sorted %#",self.sortedKeys);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.sortedKeys count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.sortedKeys objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:section]];
return [listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.accessoryType = indexPath.section > 0 ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
NSLog(#"indexPath.section %d",indexPath.section);
if ( indexPath.section==0 )
{
// cell.imageView.image = [UIImage imageNamed:#"icon.png"];
}
//cell.switch.hidden = indexPath.section > 0;
return cell;
}
Thanks in Advance!
Don't use sortedArrayUsingSelector:. Instead, explicitly create the key array in the order that you want. Then, create tableContents using that key array and another array created to hold all of your content arrays (using dictionaryWithObjects:forKeys:).
NSArray *keys = #[ #"About", #"Payment Preferences", #"Profile & Preferences", #"Security" ];
NSArray *values = #[ arrTemp1, arrTemp2, arrTemp3, arrTemp4 ];
self.tableContents = [NSDictionary dictionaryWithObjects:values forKeys:keys];
self.sortedKeys = keys;
Developing a iPAD app, where I'm trying to populate table, This is how i'm doing:
In view did load, I'm creating two dictionary object and adding object as Pdf and Excel.
- (void)viewDidLoad
{
[super viewDidLoad];
arrDocuments = [[NSMutableArray alloc] init];
NSArray *arr1 = [NSArray arrayWithObjects:#"PDF", nil];
NSDictionary *pdf = [NSDictionary dictionaryWithObjects:arr1 forKeys:nil];
NSArray *arr2 = [NSArray arrayWithObjects:#"Excel", nil];
NSDictionary *excel = [NSDictionary dictionaryWithObjects:arr2 forKeys:nil];
[arrDocuments addObject:pdf];
[arrDocuments addObject:excel];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [arrDocuments count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
//return [arrReceipes count];
NSDictionary *dictionary = [arrDocuments objectAtIndex:section];
NSArray *array = [dictionary objectForKey:[self.sortedKeys objectAtIndex:section]];
return [array count];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableView *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell ==nil) {
cell = [[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:MyIdentifier];
}
return cell;
}
With this implementation I'm not able to see table view.Its going to main method and showing me SIGABRT message.Where I'm going wrong??
While creating NSDictionary, you have passed nil in place of keys array,
NSDictionary *pdf = [NSDictionary dictionaryWithObjects:arr1 forKeys:nil];
You should give proper objects and keys to the dictionary. This method expects objects and keys to be given in an array as you can see in its method signature.
+ (instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys
I am trying to display plist data in tableview.
My problem is that after scrolling down i lost my previously fetched data.
Actually i am using custom cell so i think this is happen.
Code for Source and delegate of table is below.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellID = #"Cell";
OffersCustomCell *cell = (OffersCustomCell *)[mytableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil)
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"OffersList" ofType:#"plist"];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"OffersCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.titleLabel.font = [UIFont fontWithName:#"Formata-Regular" size:13.0f];
cell.saveLabel.font = [UIFont fontWithName:#"Formata-Bold" size:14.0f];
cell.nowLabel.font = cell.titleLabel.font;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSDictionary *dict = [tableData objectAtIndex:indexPath.row];
NSLog(#"%#",dict);
cell.titleLabel.text = [dict objectForKey:#"title"];
cell.nowLabel.text = [dict objectForKey:#"price"];
cell.saveLabel.text = [dict objectForKey:#"rondel"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[mytableView deselectRowAtIndexPath:indexPath animated:YES];
}
Did you set the height for each cell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = 0;
//calculate height for each cell
return height;
}
Use this UITableView delegate method to set height of the cells.
you can use another class that is store your data and reload time you used that class and restore your data correctly and efficiently.
I solved it. :)
only problem is no retaining memory for UItableview.
tableData = [[NSArray arrayWithObjects:[dic objectForKey:#"Response"], nil] retain];
I am trying to learn how to code a UITableView and is having some problems with the programming of the section.
I have declared 3 arrays with strings and 1 array with the 3 arrays.
firstSection = [NSArray arrayWithObjects:#"Red", #"Blue", nil];
secondSection = [NSArray arrayWithObjects:#"Orange", #"Green", #"Purple", nil];
thirdSection = [NSArray arrayWithObject:#"Yellow"];
array = [[NSMutableArray alloc] initWithObjects:firstSection, secondSection, thirdSection, nil];
To shows the headers
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString * title;
title = [NSString stringWithFormat:#"%#" , [array objectAtIndex:section]];
return title;
}
which shows the array itself as the headers
therefore is it possible to actually show the name of the sections using the names of the arrays such as firstSection and secondSection?
In your case, it's better to store your arrays in an NSDictionary. For example, if you declare and synthesize an NSDictionary variable called tableContents and an NSArray called titleOfSections, you can do something like this:
- (void)viewDidLoad {
[super viewDidLoad];
//These will automatically be released. You won't be needing them anymore (You'll be accessing your data through the NSDictionary variable)
NSArray *firstSection = [NSArray arrayWithObjects:#"Red", #"Blue", nil];
NSArray *secondSection = [NSArray arrayWithObjects:#"Orange", #"Green", #"Purple", nil];
NSArray *thirdSection = [NSArray arrayWithObject:#"Yellow"];
//These are the names that will appear in the section header
self.titleOfSections = [NSArray arrayWithObjects:#"Name of your first section",#"Name of your second section",#"Name of your third section", nil];
NSDictionary *temporaryDictionary = [[NSDictionary alloc]initWithObjectsAndKeys:firstSection,#"0",secondSection,#"1",thirdSection,#"2",nil];
self.tableContents = temporaryDictionary;
[temporaryDictionary release];
}
Then in the table view controller methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.titleOfSections count];
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
return [[self.tableContents objectForKey:[NSString stringWithFormat:#"%d",section]] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//Setting the name of your section
return [self.titleOfSections objectAtIndex:section];
}
Then to access the contents of each array in your cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *arrayForCurrentSection = [self.tableContents objectForKey:[NSString stringWithFormat:#"%d",indexPath.section]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
cell.textLabel.text = [arrayForCurrentSection objectAtIndex:indexPath.row];
return cell;
}
can u do like this
NSArray *SectionArray = [[NSArray alloc]initwithArray:[array objectAtIndex:section]];
NSString * title;
title = [NSString stringWithFormat:#"%#" , [SectionArray objectAtIndex:section]];
return title;