UITableView ObjectAtIndex - iphone

I'm developing an iPhone app that is a tabViewController with one of the tabs including a tableView. Objects in the table can be touched to go to a splash screen with information for that specific object.
Here's how I set up the array tableView:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//each table view cell has its own identifier.
static NSString *cellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [sitesArray objectAtIndex:row];
cell.imageView.image = [imagesArray objectAtIndex:row];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
- (void)viewDidLoad {
NSArray *pageHolder = [[NSArray alloc] initWithObjects:#"CSLP", #"NSLS", #"LSPA", #"SIOP", #"Transfer", nil];
self.pages = pageHolder;
[pageHolder release];
NSArray *sites = [[NSArray alloc] initWithObjects:#"CSLP", #"NSLS", #"LSPA", #"SIOP", #"Transfer Vision Program ", nil];
self.sitesArray = sites;
UIImage *cslp = [UIImage imageNamed:#"CSLP LOGO.png"];
UIImage *nsls = [UIImage imageNamed:#"NSLS LOGO.png"];
UIImage *lspa = [UIImage imageNamed:#"LSPA LOGO.png"];
UIImage *siop = [UIImage imageNamed:#"SIOP LOGO.png"];
UIImage *transfer = [UIImage imageNamed:#"TRANSFER LOGO.png"];
NSArray *images = [[NSArray alloc] initWithObjects: cslp, nsls, lspa, siop, transfer, nil];
[sites release];
self.imagesArray = images;
UIButton* modalViewButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[modalViewButton addTarget:self action:#selector(modalViewAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:modalViewButton];
self.navigationItem.rightBarButtonItem = modalBarButtonItem;
[modalBarButtonItem release];
[self.table reloadData];
}
And here's my didSelectRowAtIndexPath method
-(void) tableView:(UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath
{
NSInteger row = [indexPath row];
NSMutableDictionary *rowData = [table objectAtIndex:indexPath.row];
UIViewController *targetViewController = [rowData objectForKey:kViewControllerKey];
if (!targetViewController)
{
// The view controller has not been created yet, create it and set it to our menuList array
NSString *viewControllerName = [[pages objectAtIndex:indexPath.row] stringByAppendingString:#"ViewController"];
targetViewController = [[NSClassFromString(viewControllerName) alloc] initWithNibName:viewControllerName bundle:nil];
[rowData setValue:targetViewController forKey:kViewControllerKey];
[targetViewController release];
}
SSARC_App_2AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.orgsNavigationController pushNavigationItem:targetViewController animated:YES];
[delegate release];
}
The application crashes at the first call to objectAtIndex in the didSelectRowAtIndexPath method. The console states:
010-11-22 12:50:17.113 SSARC App
2[43470:207] -[UITableView
objectAtIndex:]: unrecognized selector
sent to instance 0x601e800 2010-11-22
12:50:17.116 SSARC App 2[43470:207]
* Terminating app due to uncaught exception
'NSInvalidArgumentException', reason:
'-[UITableView objectAtIndex:]:
unrecognized selector sent to instance
0x601e800'
* Call stack at first throw:
Now, I have a warning that says "UITableView may be unresponsive to ObjectAtIndex". I'm assuming this is the main issue here, however I'm unsure how to solve it. Interestingly enough I've searched for this issue everywhere, and cannot find it anywhere, so I'm curious to know if anyone has run into this problem and how to solve it. Any advice would be great. Let me know if you need more information.
I've been doing some coding on my own and I've gotten it to compile without any errors or warnings, but it still crashes. Currently this is what I have:
NSMutableDictionary *rowData = [self.menuList objectAtIndex:indexPath.row];
UIViewController *targetViewController = [rowData objectForKey:kViewControllerKey];
if (!targetViewController)
{
// The view controller has not been created yet, create it and set it to our menuList array
NSString *viewControllerName = [[pages objectAtIndex:indexPath.row] stringByAppendingString:#"ViewController"];
targetViewController = [[NSClassFromString(viewControllerName) alloc] init];//WithNibName:viewControllerName bundle:nil];
NSLog(#"targetViewController: %#", targetViewController);
[rowData setValue:targetViewController forKey:kViewControllerKey];
[targetViewController release];
}
SSARC_App_2AppDelegate *delegate = (SSARC_App_2AppDelegate*)[[UIApplication sharedApplication] delegate];
[[delegate orgsNavigationController] pushViewController:targetViewController animated:YES];
[delegate release];
delegate = nil;
The issue that the debugger reports is that 'viewControllerName' is 'Out of Scope'. I don't really understand what that means because when I use NSLog, viewControllerName is initialized. Does that mean it cannot find the class I'm referring to? Please let me know.

Assuming table is your UITableView, the call to [table objectAtIndex:indexPath.row] is completely nonsensical. It looks to me like you should be calling [pages objectAtIndex:indexPath.row] instead.

objectAtIndex should be called either for NSMutableArray Or NSArray. You cann't call it from UITableview Or CFDictionary Or CFString. So if you gets any warning Solve the warning first and then proceed further.

Related

NSRangeException Error with NSArray - While setting image view in another ViewController

I'm getting an image from the AppDelegate, then setting it to the ViewController's table.imageView property. It's throwing me an NSRangeException:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (2) beyond bounds (2)'
I made sure my array and rows are counted by [array count]. Very confused. Here's the code:
#pragma mark - viewWillAppear
- (void)viewWillAppear:(BOOL)animated {
PBAppDelegate *dataObject = (PBAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *titleRead = dataObject.title;
NSString *descRead = dataObject.desc;
UIImage *imageRead = dataObject.image;
if ([titleRead isEqualToString:#"" ] || [descRead isEqualToString:#""]) {
// it's nil
} else {
if (titleRead) {
[data addObject:[NSArray arrayWithObjects:titleRead, descRead, imageRead, nil]];
dataObject.title = #"";
dataObject.desc = #"";
[tableView reloadData];
}
NSUserDefaults *dataDefaults = [NSUserDefaults standardUserDefaults];
[dataDefaults setObject:[NSArray arrayWithArray:data] forKey:#"dataArrayKey"];
[dataDefaults synchronize];
}
}
#pragma mark - viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
data = [[NSMutableArray alloc] init];
self.data = [[NSUserDefaults standardUserDefaults] objectForKey:#"dataArrayKey"];
[tableView reloadData];
}
#pragma mark - Table Datasource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [[data objectAtIndex:indexPath.row] objectAtIndex:0];
cell.detailTextLabel.text = [[data objectAtIndex:indexPath.row] objectAtIndex:1];
cell.imageView.image = [UIImage imageNamed:[[data objectAtIndex:indexPath.row] objectAtIndex:2]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
It's probably this line:
cell.imageView.image = [UIImage imageNamed:[[data objectAtIndex:indexPath.row] objectAtIndex:2]];
The console output says there are 2 elements in the array. Elements start at 0; so you can access objectAtIndex:0 and objectAtIndex:1. Two would be the 3rd element of the array, and is out of bounds.
Sorry if that's all obvious, just taking a quick stab... Enjoy. :)
EDIT
Actually, the issue could be that imageRead is nil when you add it to the array. That would cause the array to have only 2 elements in it. You may check for that, and/or use [NSNull null] if you don't have an image...
Make sure the array actually has 3 elements by doing something like this:
NSArray *array = [data objectAtIndex:indexPath.row];
if ([array count] > 2)
{
cell.imageView.image = [UIImage imageNamed:[array objectAtIndex:2]];
}

self performSelector:#selector(loadData) withObject:nil - not working

I use:
self performSelector:#selector(loadData) withObject:nil...
it look like working with some command only in "loadData" but the rest are not.
Here is my viewdidload:
- (void)viewDidLoad
{
[super viewDidLoad];
[mActivity startAnimating];
[self performSelector:#selector(loadData) withObject:nil afterDelay:2];
//[mActivity stopAnimating];
}
and here is loadData:
-(void)loadData
{
[mActivity startAnimating];
NSLog(#"Start LoadData");
AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *selectData=[NSString stringWithFormat:#"select * from k_proverb ORDER BY RANDOM()"];
qlite3_stmt *statement;
if(sqlite3_prepare_v2(delegate.db,[selectData UTF8String], -1,&statement,nil)==SQLITE_OK){
NSMutableArray *Alldes_str = [[NSMutableArray alloc] init];
NSMutableArray *Alldes_strAnswer = [[NSMutableArray alloc] init];
while(sqlite3_step(statement)==SQLITE_ROW)
{
NSString *des_strChk= [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,3)];
if ([des_strChk isEqualToString:#"1"]){
NSString *des_str= [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,4)];
[Alldes_str addObject:des_str];
}
}
Alldes_array = Alldes_str;
Alldes_arrayAnswer = Alldes_strAnswer;
}else{
NSLog(#"ERROR '%s'",sqlite3_errmsg(delegate.db));
}
listOfItems = [[NSMutableArray alloc] init];
NSDictionary *desc = [NSDictionary dictionaryWithObject:
Alldes_array forKey:#"description"];
[listOfItems addObject:desc];
//[mActivity stopAnimating];
NSLog(#"Finish loaData");}
it give me only printing 2 line, but did not load my Data to the table, but if I copy all the code from inside "loadData" and past in "viewDidLoad", it load the data to the table.
Any advice or help please.
A few things: If you see any NSLog output at all, then the performSelector is succeeding. You should change the title of your question.
If you are trying to load data into a table, the method should end with telling the UITableView to reloadData (or a more elaborate load using begin/end updates).
If the listOfItems is the data supporting the table, get this working first by hard-coding something like this:
-(void)loadData {
listOfItems = [NSArray arrayWithObjects:#"test1", #"test2", nil];
[self.tableView reloadData];
return;
// keep all of the code you wrote here. it won't run until you remove the return
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *string = [listOfItems objectAtIndex:indexPath.row];
cell.textLabel.text = string;
return cell;
// keep all of the code you probably wrote for this method here.
// as above, get this simple thing running first, then move forward
}
Good luck!

NSArray and tableview

I'm making a navigation-based application, where i have a file called DataService.m and my RootViewController
.m
in my DataService.m I'm calling
-(NSArray*)loadData
{
AlarmItem *item1 = [[[AlarmItem alloc] initWithTitle:#"TEST2"] autorelease];
AlarmItem *item2 = [[[AlarmItem alloc] initWithTitle:#"TEST3"] autorelease];
AlarmItem *item3 = [[[AlarmItem alloc] initWithTitle:#"TEST4"] autorelease];
AlarmItem *item4 = [[[AlarmItem alloc] initWithTitle:#"TEST5"] autorelease];
AlarmItem *item5 = [[[AlarmItem alloc] initWithTitle:#"TEST6"] autorelease];
NSMutableArray *items = [NSMutableArray arrayWithObjects:item1, item2, item3, item4, item5, NULL];
return items;
}
and in my RootViewCoontroller.m i have
- (IBAction)RefreshAlarmList:(id)sender
{
XMLDataService *myXML = [[XMLDataService alloc] init];
[myXML loadData];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self action:#selector(RefreshAlarmList:)];
self.navigationItem.rightBarButtonItem = refreshButton;
[refreshButton release];
}
When I run my simulator the tableView is empty.. how can i display my items ?
Dont autorelease the function after it load to the tableview then autorelease it.Then how u are displaying the data to the tableview.give action as reload data in tableview.
You can try one thing.Here in the delegate method cellForRowAtIndexPath , in last u should return the cell.It may work.
(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] autorelease]; } // Configure the cell. AlarmItem *item = [_items objectAtIndex:indexPath.row]; cell.textLabel.text = item.tagName;return cell;}
[myXML loadData];
This is returning an array, which presumably you wish to use as your table's data source, but you are doing nothing with it. You need to assign this to an array, which is a retained ivar in your view controller class, _items or items judging by your other comments:
self.items = [myXML loadData];

Why aren't my NSMutableArray objects being added to the UITableView straight away?

I have an NSMutableArray with objects within it and I use the following code to add/remove objects from the UITableView. It works but only after closing and relaunching the app, not straight away. Why is this? Here is my code (maincelltext is the title in the cell and subtitlecelltext is the subtitle):
maincelltext = [[NSMutableArray alloc] init];
subtitlecelltext = [[NSMutableArray alloc] init];
[maincelltext addObject:#"TITLE"];
[subtitlecelltext addObject:#"SUBTITLE TEST"];
EDIT: Here is my UITableView code:
- (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];
}
cell.textLabel.font = [UIFont fontWithName:#"HelveticaBold" size:16.0];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.numberOfLines = 1;
// Configure the cell.
UIImage *cellImage = [UIImage imageNamed:#"warning.png"];
CGRect cropRect = CGRectMake(0.0, 0.0, 12.0, 12.0);
CGImageRef croppedImage = CGImageCreateWithImageInRect([cellImage CGImage], CGRectMake(0.0f,0.0f,cellImage.size.width,cellImage.size.height));
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:cropRect];
[myImageView setImage:[UIImage imageWithCGImage:croppedImage]];
CGImageRelease(croppedImage);
[self.view addSubview:myImageView];
cell.imageView.image = cellImage;
NSString *subjectString = [maincelltext objectAtIndex: [indexPath row]];
cell.textLabel.text = subjectString;
NSString *subtitle = [subtitlecelltext objectAtIndex: [indexPath row]];
cell.detailTextLabel.text = subtitle;
if(maincelltext.count == 0){
NSString *notabledata = [NSString stringWithFormat:#"No Dates Set"];
[maincelltext addObject:notabledata];
}
return cell;
}
Thanks!
you need to tell the UITableView that it needs to insert the rows. First determine the index in the array the new object(s) are now at, then tell the UITableView to insert the rows like this:
NSIndexPath *indexPathOfNewObject=[NSIndexPath indexPathForRow: newObjectIndex section:0];
NSArray *indexPathArray=[NSArray arrayWithObject: indexPathOfNewObject];
[self.tableView beginUpdates];
// Note that UITableViewRowAnimationAutomatic is for iOS5 only.
[self.tableView insertRowsAtIndexPaths: indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
by using this method, you will get the cool animation effect on the tableView.
Alternatively, you could just call [self.tableView reloadData] and it will just reload all the data.
Good luck!
Just do a [myTableView reloadData]; after you did an [myArray addObject:myObject]; and you should be fine.

iphone slow navigation due to data load

I'm newbie to objective-c . My problem is slow navigation between controllers.
When I jump from one controller to another it's really slow, because it gets data from the service, parse it and then display it. All this takes around 4-5 secs.
Now I've put all this data fetching in loadView of my DetailController. Is there anyway that I show the controller first and then display the data. Because as of now, when I select a row on my root controller, it shows activity and stays on root controller and then it navigates to the DetailController.
So it loads the data first and then displays it. Is there anyway that I can reverse this process, show the controller first and then display it..
this is what I'm doing
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DataCollection *collection = [DataCollection sharedObject];
Product *tempProduct = [[collection popularProducts] objectAtIndex:row] ;
ProductDetailViewController *tempProductDetail = [[ProductDetailViewController alloc] init];
tempProductDetail.homeViewProduct = tempProduct;
[tempProductDetail release];
}
and then the Detail Controller
- (void)loadView {
[super loadView];
// here's all the data loading and parsing ... which takes time
}
//Here's the method for downloading the data, I'm using a REST service to get all the data.
- (void) getProductData:(NSString*)productId{
NSMutableString *specificURL = [[NSMutableString alloc] initWithString:#"GetProductPage?id="];
[specificURL appendFormat:#"%#",productId];
[specificURL appendFormat:#"&dataSources=&accountId=&companyId=&version=1&cultureCode=sv&currencyId=2&format=json"];
NSDictionary *serverCategories = [[NSDictionary alloc] initWithDictionary:[appRequestController proceesRequest:specificURL]];
NSArray *categories = [[[serverCategories objectForKey:#"DataSources"] objectAtIndex:0] objectForKey:#"Items"];
[serverCategories release];
if ([[productsArray objectAtIndex:j] objectForKey:#"Name"] != [NSNull null]) {
[product setProductName:[[productsArray objectAtIndex:j] objectForKey:#"Name"]];
}
else {
[product setProductName:#""];
}
if ([[productsArray objectAtIndex:j] objectForKey:#"ThumbnailImage"] != [NSNull null]) {
[product setProductImage:[[productsArray objectAtIndex:j] objectForKey:#"ThumbnailImage"]];
}
else {
[product setProductImage:#""];
}
if ([[productsArray objectAtIndex:j] objectForKey:#"Price"] != [NSNull null]) {
[product setProductPrice:[[productsArray objectAtIndex:j] objectForKey:#"Price"]];
}
else {
[product setProductPrice:#""];
}
[[collection popularProducts] addObject:product];
[product release];
}
I've copy pasted junks of the code so that you can get an idea how I'm getting the data. Below is the code for cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *MyIdentifier = #"mainMenuIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
[cell setSelectedBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"highlightstrip.png"]]];
}
UILabel *productNameLbl = [[UILabel alloc] initWithFrame:CGRectMake(90, 8, 200,10)];
[productNameLbl setText:[NSString stringWithString:[[[[DataCollection sharedObject] popularProducts]objectAtIndex:indexPath.row] productName]]];
[productNameLbl setFont:[UIFont boldSystemFontOfSize:12.0]];
[cell addSubview:productNameLbl];
[productNameLbl release];
UILabel *productSubLbl = [[UILabel alloc] initWithFrame:CGRectMake(90, 22, 190,40)];
[productSubLbl setText:[NSString stringWithString:[[[[DataCollection sharedObject] popularProducts]objectAtIndex:indexPath.row] productSubHeader]]];
[productSubLbl setTextColor:[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102/255.0 alpha:1.0]];
productSubLbl.lineBreakMode = UILineBreakModeWordWrap;
productSubLbl.numberOfLines = 3;
[productSubLbl setFont:[UIFont fontWithName:#"Arial" size:10]];
[cell addSubview:productSubLbl];
[productSubLbl release];
NSMutableString *url = [[NSMutableString alloc] initWithString:#""];
NSString *baseImageURL = #"http://samplesite.com/";
NSString *imagePath = [NSString stringWithString:[[[[DataCollection sharedObject] popularProducts]objectAtIndex:indexPath.row] productImage]];
[url appendString:baseImageURL];
[url appendString:imagePath];
NSURL *imageURL = [NSURL URLWithString:url];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *img = [[UIImage alloc] initWithData:data cache:NO];
UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
[imageView setFrame:CGRectMake(10, 10, 70, 70)];
[cell addSubview:imageView];
[imageView release];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Pass the url or product id to the detail view. Inside the viewDidLoad of the ProductDetail, create a background thread to retrieve your data. When you background thread finish, it will notify your ProductDetail controller with the detail. At that time, you update your ProductDetailView.
To get your data on a background thread you can use
NSThread
NSOperation
Pass the row into your ProductDetailViewController, and do the fetch in it's viewDidLoad method. You might also want to show a spinner (UIActivityIndicator), and you definitely should read up on background threads.
To make view available to user and loading data after some time you can use NSTimer.
Just have a NSTimer method which will be called in viewDidLoad method as
[NSTimer scheduledTimerWithTimeInterval:<#(NSTimeInterval)ti#> target:<#(id)aTarget#> selector:<#(SEL)aSelector#> userInfo:<#(id)userInfo#> repeats:<#(BOOL)yesOrNo#>]
put some time interval which will be calling your loading method after that.
Hope this helps.