I have a class that extends UITableviewController which displays a data type called "GigData" (which only contains strings for now). The content is stored in "data" which is an NSMutableArray containing NSMutableArrays containing "GigData". This array is passed to the instance of my class and the arrays inside arrays make up the sections of the table. Here is the code I have implemented so far:
#synthesize data = _data;
- (id)init
{
self = [super initWithStyle:UITableViewStyleGrouped];
_data = [[NSMutableArray alloc] init];
[[self navigationItem] setTitle:#"Gigs by Date"];
return self;
}
- (id)initWithStyle:(UITableViewStyle)style
{
return [self init];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [_data count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [[_data objectAtIndex:section] count];
}
- (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];
}
NSMutableArray *sectionArray = [_data objectAtIndex:[indexPath section]];
GigData *gig =[sectionArray objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[gig description]];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
GigData *temp = [[_data objectAtIndex:section] objectAtIndex:0];
return [temp date];
}
When I run the app, I can see everything sorted into the right groups and all the displays are correct, except for the final section, which keeps changing names, some of which have included "cs.lproj", "headers" and "method not allowed". Scrolling to the bottom of the table then towards the top crashes the app. Also, if I provide my own implementation for description for "GigData", the app crashes even worse, I cannot scroll to the second section at all. Data is declared as a property in the header file and is set to nonatomic and retain. I have also tried using the code inside the init method to create the data array inside this class, but this makes no difference. Some runnings of the app have said there is a problem in tableView:cellForRowAtIndexPath: when I create "sectionArray". Has any body got any suggestions as to what I am doing wrong?
Related
I am creating an iPhone game which will call a generic Newsfeed and apply with a random share, affecting the share price with a low medium or high increase/decrease. It was suggested I use parallel arrays to go about this. Is that the best method? And has anyone got a useful link for tutorials in how to do so?
I have a simple array setup. But I am new to x-code and I need to get something coded to call items from the array.
#import "NewsFeedViewController.h"
#interface NewsFeedViewController ()
#end
#implementation NewsFeedViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Setup NSArray
items = [[NSArray alloc] initWithObjects:#"Galaxy", #"Redbull", #"Boost", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.textLabel.text = [items objectAtIndex:indexPath.row];
return cell;
}
#end
If you are looking for Parallel array, then this is an example :
NSArray *alphabets=#[#"A",#"B",#"C",#"D"];
NSArray *words=#[#"Apple",#"BlackBerry",#"Cognizant",#"Dell"];
for (NSInteger i=0; i<alphabets.count; i++) {
NSLog(#"%# for %#",alphabets[i],words[i]);
}
It will be clear that, two arrays are related by 1-1 mapping, and while looping, I used same index to match the record.
I just answered an unrelated question, but the project i built uses three arrays as datasources for a TableView.
Sample Project link is below, but this is the meat of the code:
https://dl.dropbox.com/u/3660978/UniversalTableView.zip
-(void)populateArrays {
// New literal syntax of interacting with Arrays.
namesArray = #[#"Homer Simpson",#"Marge Simpson",#"Bart Simpson",#"Lisa Simpson",#"Maggie Simpson",#"Mr. Burns",
#"Principal Skinner",#"Krusty the Clown"];
descriptionsArray = #[#"Homer is a lazy, but also a funny father",
#"Marge is smart, but not smart enough to not marry Homer lol",
#"Bart is just a smart-alec, but he is really funny too",
#"Lisa is the smartest one and the brunt of most jokes",
#"Maggie is just the baby of the family",
#"Mr. Burns is Homer's boss and evil too",
#"Principal Skinner is Bart and Lisa's principal at school",
#"Krusty is always clowning around (not really that funny though)"];
imagesArray = #[#"homer.jpg",#"marge.jpg",#"bart.jpg",#"lisa.jpg",#"maggie.jpg",#"burns.jpg",
#"skinner.jpg",#"krusty.jpg"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [namesArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCustomCell";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.customLabel.text = [namesArray objectAtIndex:indexPath.row];
cell.customTextView.text = [descriptionsArray objectAtIndex:indexPath.row];
cell.customImageView.image = [UIImage imageNamed:[imagesArray objectAtIndex:indexPath.row]];
return cell;
}
I created an app which will fetch info from web service.So far i got it by displaying the contents using NSLog but when i tried to load it in UITableViewCell its not displayed.Here is my code for that
#import "TableViewController.h"
#import "JSON.h"
#import "SBJsonParser.h"
#interface TableViewController ()
#end
#implementation TableViewController
#synthesize jsonurl,jsondata,jsonarray;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
jsonurl=[NSURL URLWithString:#"http://minorasarees.com/category.php"];
jsondata=[[NSString alloc]initWithContentsOfURL:jsonurl];
self.jsonarray=[jsondata JSONValue];
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [jsonarray count];
}
- (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];
}
NSLog(#"1");
NSLog(#"%#",jsonarray);
cell.textLabel.text=[jsonarray objectAtIndex:indexPath.row];
NSLog(#"2");
return cell;
}
-(void)dealloc
{
[super dealloc];
[jsonarray release];
[jsondata release];
[jsonurl release];
}
#end
i've inserted tableviewcontroller by adding file with UITableViewController..will that be a problem..Help please..
Your JSON contains an array of dictionaries, so you're setting the text in your table view cell to an dictionary which cannot work since a string is expected. This actually should crash.
To solve that set your text to the category property of that dictionary:
cell.textLabel.text=[[jsonarray objectAtIndex:indexPath.row] valueForKey: #"category"];
besides this there are other things wrong with your code: [super dealloc] needs to be the last thing you call in your dealloc method. And you really should be using asynchronous networking code, blocking the main thread with networking is not acceptable.
So, i have UITableView, large amount of data which is displayed with a many rows, and i want to make sections (like default contacts application and its sections). So there is my code (listViewController.m file):
#import "FailedBanksListViewController.h"
#import "FailedBankDatabase.h"
#import "FailedBankInfo.h"
#import "FailedBanksDetailViewController.h"
#import "BIDAppDelegate.h"
#implementation FailedBanksListViewController
#synthesize failedBankInfos = _failedBankInfos;
#synthesize details = _details;
- (void)viewDidLoad {
self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"3.png"]];;
[super viewDidLoad];
self.failedBankInfos = [FailedBankDatabase database].failedBankInfos;
self.title = #"Продукты";
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.failedBankInfos = nil;
self.details = nil;
}
- (void) viewWillAppear:(BOOL)animated
{
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [_failedBankInfos count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_failedBankInfos 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:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Set up the cell...
FailedBankInfo *info = [_failedBankInfos objectAtIndex:indexPath.row];
cell.textLabel.text = info.name;
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#, %#", info.city, info.state];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.details == nil) {
self.details = [[FailedBanksDetailViewController alloc] initWithNibName:#"FailedBanksDetailViewController" bundle:nil];
}
FailedBankInfo *info = [_failedBankInfos objectAtIndex:indexPath.row];
_details.uniqueId = info.uniqueId;
[self.navigationController pushViewController:_details animated:YES];
}
- (void)dealloc {
self.failedBankInfos = nil;
}
#end
With your code you should have multiple sections (each one exactly equal than the others).
The idea for a multiple section table view is (normally) to have a 2 dimensional array (not 1 dimensional as is your case). Then each row would represent a section for your table view.
For example, if you have an array structured this way (and I know you can't initialize it this way):
arr = {
{'apple','orange','banana'},
{'CD-Rom', 'DVD', 'BR-Disk'},
{'AK-47', 'Rocket launcher', 'Water gun'}
}
your number of sections method may return [arr count] and the number of rows for section s may return [[arr objectAtIndex:s] count]. And remember that you can set the title for each section with the table view datasource method tableView:titleForHeaderInSection:.
If you want to load the info from a SQLite DB, nothing may change. It's exactly the same but you will have to keep of the way to get your data.
When you thing you understand all this stuff then checkout the Core Data framework.
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 can't display array in UITableView. The thing I do is - in viewWILLappear I'm creating array. In viewDIDappear I'm filling the array. But when I run [myArr count] or [myArr objectAtIndex:indexPath.row] in table setup I get empty table. If I define constant integer as row count and some constant string as cell text everything works fine. Is there some populate() method I have to run or is it a problem with some order of declarations?
Thanks for any help. Here's the code:
- (void)viewWillAppear:(BOOL)animated {
myArr = [[NSMutableArray alloc] initWithCapacity:100];
}
- (void)viewDidAppear:(BOOL)animated {
[self load_array];
}
- (void) load_array {
for (SomeObject *someObject in SomeObjects) {
[myArr addObject:someObject.someString];
NSLog(#"Value: %#", [myArr objectAtIndex:([myArr count]-1)]); // works
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [myArr count]; // works if I return const ("return 2")
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (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];
}
cell.textLabel.text = [myArr objectAtIndex:indexPath.row]; //=#"ASDF" works.
return cell;
}
You need to perform reloadData on your table view to make the view re-load the table cells.
Update: You should not allocate your array in the viewWillAppear, as this method might be called several times. Construct the array in the viewDidload: and fill it there, or in a background thread, or in the viewWillAppear: (using a conditional statement to check if its already filled). You should also make sure that you do not create a memory leak, from the code you provided it is likely that myArr will be replaced by a newly allocated array without being released.