I have a UITableView for which I have listed the code below.
How do I section these objects out such as the following shown below?
Math
Algebra
Geometry
Calculus
Science
Integrated 1
Integrated 2
Here is the code:
- (void)viewDidLoad {
NSArray *array = [[NSArray alloc] initWithObjects:#"Math",#"Science",#"English",#"Social Studies",#"Spanish",#"German",#"French",#"Biology",nil];
self.listData = array;
[array release];
[super viewDidLoad];
}
You have to use sectioned table view, that is the best way to achieve this. You can refer following tutorial for the same - http://www.xcode-tutorials.com/uitableview-–-3-sectioned-table-view/
In your UITableViewDataSource of UITableView object;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return array.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [array objectAtIndex:section];
}
will do the 'sectioning' part for your case.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [yourarray count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [yourarray objectAtIndex:section];
}
Related
I'm using a UITableView with a subclassed prototype cell with a UITextField. The cell is used 4 times total in 4 sections. This is the code for it:
...
- (void)viewDidLoad
{
[super viewDidLoad];
cellTitles = [[NSArray alloc] initWithObjects:#"Smell", #"Taste", #"Suits", #"Notes", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [cellTitles count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [cellTitles count] / [cellTitles count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [cellTitles objectAtIndex:section];
}
- (AddInfoCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"AddInfoCell";
AddInfoCell *addInfoCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (addInfoCell == nil) {
addInfoCell = [[AddInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
addInfoCell.cellTextView.delegate = self;
return addInfoCell;
}
I want to add the text from each cell to the dataSource object:
-(IBAction)nextButtonPressed:(id)sender {
...
[[dataSource objectAtIndex:dataSourceIndex] setObject:* forKey:#"Smellnote"];
[[dataSource objectAtIndex:dataSourceIndex] setObject:** forKey:#"Tastenote"];
//etc
…
}
*text from cell 1
**text from cell 2
How can I access the text that I want?
Maybe you're looking at it wrong. Why not make the delegate for the textField be the cell itself, ie, in the Cell's class, set the delegate to File'sOwner or self in the init... method.
Create a reference to the dataSource and pass the dataSource to your cell, along with the #"Key" for your dictionary.
cell.dataSource = self.dictionary;
cell.dataKey = #"Tastenote";
//in cell .m
//textFieldDelegate method...
[self.dataSource setObject:self.cellTextView.text forKey:self.dataKey];
I have table and one global variable in appdelegate class. I store all date in this global array and showing this array value in controller in table view. I have to give section index with alphabetical order by short how to give please help me on this
I tried this code but it not working for me
Kindly help me.
h.file:
NSMutableArray *timeZonesArray;
NSMutableArray *sectionsArray;
UILocalizedIndexedCollation *collation;
.mfile
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
//return 1;
return [[collation sectionTitles] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
//app.networkActivityIndicatorVisible = YES;
NSArray *timeZonesInSection = [app.lstAirports objectAtIndex:section];
return [timeZonesInSection count];
// return app.lstAirports.count;
}
// 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:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
NSArray *timeZonesInSection = [app.lstAirports objectAtIndex:indexPath.section];
a=(airport*)[timeZonesInSection objectAtIndex:indexPath.row];
NSLog(#"str:%#",a);
if(a!=nil){
cell.textLabel.text =a.Name;
cell.detailTextLabel.text=a.Code;
//[app.spinner stopAnimating];
}
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[collation sectionTitles] objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [collation sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [collation sectionForSectionIndexTitleAtIndex:index];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
app.originAirport = (airport*)[app.lstAirports objectAtIndex:indexPath.row];
[delegate Originstart:app.originAirport forIndexPath:self.indexPathToChange];
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark Set the data array and configure the section data
- (void)setTimeZonesArray:(NSMutableArray *)newDataArray {
if (newDataArray != timeZonesArray) {
[timeZonesArray release];
timeZonesArray = [newDataArray retain];
}
if (timeZonesArray == nil) {
self.sectionsArray = nil;
}
}
In order to display/arrange your data in alphabetical which in a array you have to use NSSortDescriptor
you have to make the object of this NSSortDescriptor class and give data here which you are fetching from XML
NSSortDescriptor *itemXml = [[NSSortDescriptor alloc] initWithKey:#"itemName" ascending:YES];
Now suppose you have an array sortDescriptors
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:itemXml,nil];
[yourArray sortUsingDescriptors:sortDescriptors];
I hope this will be usefull to you.
If you are looking for section-wise sorting I would hold your various sections in a dictionary that holds arrays for each of the sections and then sort each of those arrays as needed. It requires a bit more code on the table delegate methods but allows for complete control over each section's data
Another option is to encapsulate your data in a different and add a property for the section you want it in and add another sort descriptor to that so it is sorting the array by section and then by any other value. You do that by just adding another NSSortDescriptor to the array in the order you want the array to be sorted by.
The perfect example , please refer the code,
Sorting Alphabetically
I am trying to create an Indexed UITableView with out the magnifying glass icon.
I realize the UITableViewIndexSearch is creating the icon in the index but I do not know what to replace it with. Any help or suggestions would be greatly appreciated.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.fetchedResultsController sections] count];}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return index;}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
// Return the array of section index titles
NSArray *searchArray = [NSArray arrayWithObject:UITableViewIndexSearch];
return [searchArray arrayByAddingObjectsFromArray:self.fetchedResultsController.sectionIndexTitles];}
Simply replace it with nothing. Just leave the UITableViewIndexSearch constant out of your array.
Don't add UITableViewIndexSearch in sectionIndexTitlesForTableView:. Simply return the array from your fetchedResultsController.
I was working with the tableview and I wanted the number of rows (i.e the number of items in tableview). How can I get the number of rows, if available?
Please help me out,
thanks in advance.
take a instance variable in your header file.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
yourHeaderFileVariable = [noOfItemsInArray count];
NSLog(#"total item in table %d",yourHeaderFileVariable);
return [noOfItemsInArray count];
}
or try this
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
NSLog(#"total item in table %d",[noOfItemsInArray count]);
return [noOfItemsInArray count];
}
The number of rows in table view for a section is what you are returning from (NSInteger)tableView:numberOfRowsInSection:. So if you return something like [mySource count] from this method then the number of rows is [mySource count].
For example:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [data count];
}
// to get the count
NSInteger rowCount = [data count];
Now replace [data count] with whatever calculation you have in the delegate method.
You need to implement the delegate method :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [yourArray count];
}
You can then show the result in the table header by using the following delegate method :
- (NSString *)tableView:(UITableView *)resultTableView titleForHeaderInSection:(NSInteger)section
{
NSString *str=[NSString stringWithFormat:#"%i",[yourArray count]];
str=[str stringByAppendingString:#" matches"];
return str;
}
For example, if you have 7 results in the table, the header will display the text "7 matches"
- (NSInteger)tableView:(UITableView *)tablefirst numberOfRowsInSection:(NSInteger)section {
int count;
count=0;
count=[*your array name* count];
NSLog(#"row count=%d",count);
return [*your array name* count];
}
This function returns the number rows(items) in your tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [listOfItems count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return numberOfRows;
}
Iam adding objects to NSMutableArray and displaying the value in UITableView. Only the first object of the NSMutableArray is getting displayed.Where am i going wrong?
My Code snippet:
-(IBAction) segmentedControlIndexChanged:(id)sender {
int selectedSegment = ((UISegmentedControl*) sender).selectedSegmentIndex;
switch (selectedSegment) {
case 1: {
MatchSchedule *semiFinal1 = [[MatchSchedule alloc] initWithDeails:#"20 March"];
MatchSchedule *semiFinal2 = [[MatchSchedule alloc] initWithDeails:#"24 March"];
matchList = [[NSMutableArray alloc] initWithObjects:semiFinal1,semiFinal2,nil];
[semiFinal1 release];
[semiFinal2 release];
break;
}
default;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
....
MatchSchedule *matchSchedule = [knockoutStage objectAtIndex:indexPath.row];
cell.textLabel.text = matchSchedule.matchDate;
}
Hard to tell without seeing more of your code. Are you returning the correct value for
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [knockoutStage count];
}
?