I parse an product ID via JSON which is then pushed to a detail view and put into an url to parse more information about the selected product but it seems like didSelectRowAtIndexPath returns a wrong indexPath because every time I hit a row it doesn't load the product for the detail view gets the product ID of the row which was retrieved last.
For example I have 5 rows displayed. My logic parses all ids correctly for each row (row 1, row 2, row 3, row 4, row 5). I know hit the first row because I want to get the information linked to row 1 but instead of the information of row one the detail view gets the command to parse information for row 5 because that is the last id which was parsed and then displays the wrong information.
I don't know how to make the didSelectRowAtIndexPath method get the correct information for the correct row.
Here is my Code for didSelectRowAtIndexPath:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIStoryboard *iPhone = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
CJArtikelDetailViewController *artikelView = [iPhone instantiateViewControllerWithIdentifier:#"detail"];
NSString *detail = [NSString stringWithFormat:#"%#", wareID];
artikelView.productID = detail;
[self.navigationController pushViewController:artikelView animated:YES];
[neuheitenTableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(#"IndexPath: %#", [indexPath description]);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *neuheitenCell = #"neuheitenCell";
CJHomeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:neuheitenCell];
/*
NSNumber *preis = [[arrayArtikelPreis objectAtIndex:indexPath.row] objectForKey:#"preis"];
NSMutableString *test = [NSMutableString stringWithFormat:#"€ %#", preis];
cell.artikelPreis.text = test;
*/
NSString *imageString = [[arrayNeuheiten objectAtIndex:indexPath.row] objectForKey:#"bild"];
//NSLog(#"BildURL: %#", imageString);
NSURL *imageURL = [NSURL URLWithString:imageString];
[cell.artikelImage setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:#""]];
NSDictionary *object = [arrayNeuheiten objectAtIndex:indexPath.row];
//NSLog(#"%#", object);
wareID = [object objectForKey:#"ware_id"];
NSLog(#"Waren ID: %#", wareID);
cell.artikelName.text = [object objectForKey:#"name"];
cell.artikelHersteller.text = [object objectForKey:#"lieferant_name"];
return cell;
}
I have to make the method somehow determine which row was selected but I don't know how and couldn't find information about it in the docs.
Thanks in advance for any help!
Hope this helps
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIStoryboard *iPhone = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
CJArtikelDetailViewController *artikelView = [iPhone instantiateViewControllerWithIdentifier:#"detail"];
NSDictionary *object = [arrayNeuheiten objectAtIndex:indexPath.row];
//NSLog(#"%#", object);
//assign selected product id to wareId here and pass it to detail view controller it will work
wareID = [object objectForKey:#"ware_id"];
NSLog(#"Waren ID: %#", wareID);
NSString *detail = [NSString stringWithFormat:#"%#", wareID];
artikelView.productID = detail;
[self.navigationController pushViewController:artikelView animated:YES];
[neuheitenTableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(#"IndexPath: %#", [indexPath description]);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *neuheitenCell = #"neuheitenCell";
CJHomeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:neuheitenCell];
/*
NSNumber *preis = [[arrayArtikelPreis objectAtIndex:indexPath.row] objectForKey:#"preis"];
NSMutableString *test = [NSMutableString stringWithFormat:#"€ %#", preis];
cell.artikelPreis.text = test;
*/
NSString *imageString = [[arrayNeuheiten objectAtIndex:indexPath.row] objectForKey:#"bild"];
//NSLog(#"BildURL: %#", imageString);
NSURL *imageURL = [NSURL URLWithString:imageString];
[cell.artikelImage setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:#""]];
//comment out wareId value assigning code here
NSDictionary *object = [arrayNeuheiten objectAtIndex:indexPath.row];//commemted this line by mistake
//NSLog(#"%#", object);
//wareID = [object objectForKey:#"ware_id"];
//NSLog(#"Waren ID: %#", wareID);
cell.artikelName.text = [object objectForKey:#"name"];
cell.artikelHersteller.text = [object objectForKey:#"lieferant_name"];
return cell;
}
Related
I have two tableViewControllers created in storyboard:
categories and stores.
When I select a category, it's supposed to show all stores that are in that category.
(I'm getting stores data from server, but I set a fixed category in the url so it should fetch the same two stores no matter what.)
The weird thing is that when I choose a category, storesController shows up empty,
but if I set it as initial controller, it works.
StoreTableViewController_as_initial.png
StoreTableViewController_from_categories.png
this is how I setup the cells in storeController.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
StoresCell *cell = [tableView dequeueReusableCellWithIdentifier:#"storetile"];
if(cell == nil){
cell = [[StoresCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"storetile"];
}
cell.storelabel.text = [[_results objectAtIndex:[indexPath row]]objectForKey:#"name"];
[cell.storelabel sizeToFit];
cell.subtitleOneLabel.text=[[_results objectAtIndex:[indexPath row]]objectForKey:#"neighborhood"];
cell.subtitleTwoLabel.text = [[_results objectAtIndex:[indexPath row]]objectForKey:#"city"];
NSString *distance = [NSString stringWithFormat:#"~%# km",[[_results objectAtIndex:[indexPath row]]objectForKey:#"distance"]];
cell.distanceLabel.text = distance;
NSString *imgPath = [[_results objectAtIndex:[indexPath row]]objectForKey:#"imgUrl"];
UIImage *thumb = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://andrafterdevelopment.com.br/apps/buscaabc/img/%#",imgPath]]]];
[cell.thumbImage setImage:thumb];
return cell;
}
I break at the return, the method is always called but when it's from categoriesController all labels in my cell are nil.
this is how I call storeController from categoriesController
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath{
NSDictionary *item = [_results objectAtIndex:[indexPath row]];
BOOL hasSub = [[item objectForKey:#"hasSub"] boolValue];
StoreTableViewController *storesTableViewController = [[StoreTableViewController alloc]init];
//storesTableViewController.title = [item valueForKey:#"name"];
//storesTableViewController.category = (int)[item valueForKey:#"id"];
[storesTableViewController.tableView registerClass:[StoresCell class] forCellReuseIdentifier:#"storetile"];
[self.navigationController pushViewController:storesTableViewController animated:YES];
}
I'm guessing this has to do with custom cell or maybe I'm doing it wrong by calling it this way.
If you use Storyboard, you should create your controller via Storyboard.
Set Storyboard ID in Identity inspector for your StoreTableViewController and change your method on:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath{
NSDictionary *item = [_results objectAtIndex:[indexPath row]];
BOOL hasSub = [[item objectForKey:#"hasSub"] boolValue];
StoreTableViewController *storesTableViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"storyboard id"];
storesTableViewController.title = [item valueForKey:#"name"];
storesTableViewController.category = (int)[item valueForKey:#"id"];
[self.navigationController pushViewController:storesTableViewController animated:YES];
}
Or you can use segues and configure your StoreTableViewController in prepareForSegue method:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"StoreTableViewControllerSegureIdentifier"]) {
StoreTableViewController *storesTableViewController = segue.destinationViewController;
storesTableViewController.title = _selectedItem.title;
storesTableViewController.category = _selectedItem.identifier;
}
}
I am guessing you have declared _results as a property in StoreTableViewController right?
If so, change your categoriesController method to:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath{
NSDictionary *item = [_results objectAtIndex:[indexPath row]];
BOOL hasSub = [[item objectForKey:#"hasSub"] boolValue];
StoreTableViewController *storesTableViewController = [[StoreTableViewController alloc]init];
storesTableViewController.title = [item valueForKey:#"name"];
storesTableViewController.category = (int)[item valueForKey:#"id"];
[self.navigationController pushViewController:storesTableViewController animated:YES];
}
And register cell in viewDidLoad.
Their are few things that i think you should check in your code.
When you are opening second view controller for selected category, did your service giving you data?
If you are getting data, then is it getting filled in array in right way?
And if everything above is fine, then had you called [tableView reloadData] method after filling data in array.
I make dictionary in iOS, I get data from JSON url. I have problem with correctly display result of word from SearchBar. For example when I type something in searchBar and click on some result , always display value of first result.
Below I put my screen to present this problem.
On the second screen I click on Ruby on Rails and show me first of result "Objective C", why (Objective C is a first result in JSON file)?
And it's my code: http://pastebin.com/EPVTpF9U
#pragma mark - Navigation
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:#"pushDetailView"]){
NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow];
DetailViewController *modalVC = (DetailViewController*)segue.destinationViewController;
modalVC.detailArray = [self.finalResultArray objectAtIndex:indexPath.row];
}
}
#pragma mark - UISearchDisplayDelegate
// register a cell reuse identifier for the search results table view
-(void)searchDisplayController:(UISearchDisplayController *)controller
didLoadSearchResultsTableView:(UITableView *)tableView {
[tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:#"Cell"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(tableView == self.tableView){
return self.finalResultArray.count;
}else{
return self.results.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdetifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdetifier forIndexPath:indexPath];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdetifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if(tableView == self.tableView){
cell.textLabel.text = [[self.finalResultArray objectAtIndex:indexPath.row] objectForKey:#"expression"];
cell.detailTextLabel.text = [[self.finalResultArray objectAtIndex:indexPath.row] objectForKey:#"meaning"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else{
cell.textLabel.text = [[self.results objectAtIndex:indexPath.row] objectForKey:#"expression"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
UIView *customColorView = [[UIView alloc] init];
customColorView.backgroundColor = [UIColor colorWithRed:180/255.0
green:138/255.0
blue:171/255.0
alpha:0.5];
cell.selectedBackgroundView = customColorView;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"pushDetailView" sender:self];
}
- (void)simpleJsonParsing
{
//-- Make URL request with server
NSHTTPURLResponse *response = nil;
NSString *jsonUrlString = [NSString stringWithFormat:#"https://uidictionary.herokuapp.com/phrases.json"];
NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//-- Get request and response though URL
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
//-- JSON Parsing
NSArray *result = [[NSJSONSerialization JSONObjectWithData:responseData options:
NSJSONReadingMutableContainers error:nil] objectForKey:#"phrases"];
[self.finalResultArray removeAllObjects];
for (NSMutableDictionary *tmp in result)
{
NSMutableDictionary *temp = [NSMutableDictionary new];
[temp setObject:[tmp objectForKey:#"expression"] forKey:#"expression"];
[temp setObject:[tmp objectForKey:#"meaning"] forKey:#"meaning"];
[self.finalResultArray addObject:temp];
}
if (self.finalResultArray){
[self.tableView reloadData];
}
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
self.results = [self.finalResultArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"%K contains[c] %#", #"expression", searchController.searchBar.text]];
NSLog(#"Filterd Array:-%#", self.results);
// hand over the filtered results to our search results table
UITableViewController *tableController = (UITableViewController *)self.searchController.searchResultsController;
tableController.tableView.dataSource = self;
tableController.tableView.delegate = self;
[tableController.tableView reloadData];
}
Just replace Bellow Code :-
self.results = [self.finalResultArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"%K BEGINSWITH[c] %#", #"expression", searchController.searchBar.text]];
#mechu911
Replace your method with below code.
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:#"pushDetailView"]){
NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow];
DetailViewController *modalVC = (DetailViewController*)segue.destinationViewController;
modalVC.detailArray = [self.result objectAtIndex:indexPath.row];
}
}
but you have to put check like
if(tableView == self.tableView){
modalVC.detailArray = [self.finalResultArray objectAtIndex:indexPath.row];}
else{modalVC.detailArray = [self.result objectAtIndex:indexPath.row];}
I am developing an ios app..Click on button to Pass The Json Array To other UiViewController TableView To Show The Data In TableView..In TableView Array Data Pass on NSDictionary and to Use Dictionary Object. Error is [__NSCFDictionary objectAtIndex:]: Unrecognised selector sent to instance 0x7b115560']...Thanks In Advance
// Button Click
BBAuthorDetailViewController *BBAuthorDetail =[[UIStoryboard storyboardWithName:#"Main" bundle:nil]instantiateViewControllerWithIdentifier:#"BBAuthorDetail"];
[BBAuthorDetail setSelectionType:BBSelectionAuthorName];
_serverObj = [[Server alloc]init];
[_params setObject:_adDetailsObj.authorDetail forKey:#"author"];
[_serverObj BBAuthorNameWithParams:_params];
// BBAuthorDetail.data=resultsArray;
//NSIndexPath *indexPath = [BBAuthorDetail.tableview indexPathForSelectedRow];
BBAuthorDetail.data = [resultsArray objectAtIndex:indexPath.row];
NSLog(#"%#",resultsArray);
//[BBAuthorDetail setManagedObjectContext:self.managedObjectContext];
[self.navigationController pushViewController:BBAuthorDetail animated:YES];
UIViewController Table TO Show Data
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat:#"Cell-%li", (long)indexPath.row];
BBAdsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// AdDetails *_adDetailsObj = (AdDetails *)[_data objectAtIndex:indexPath.row];
// NSDictionary *dic = [_data objectAtIndex:indexPath.row];
//AdDetails *_adDetailsObj = [[AdDetails alloc]init];
if (cell == nil) {
cell = [[BBAdsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.row = indexPath.row;
//[cell setDelegate:self];
}
// Error Part
NSDictionary *dic = [_data objectAtIndex:0];
cell.textLabel.text = [dic objectForKey:#"post_author"];
return cell;
}
I have a UITableView which is populated with some parsed JSON twitter data. The intent is to have the user select the a row, and have the data passed to a modalViewController, which in this case is a map displaying coordinate and annotation information.
In the debug console I can see the data loaded into each visible UITableViewCell, plus the first one off screen (last loaded). When I run the app, and attempt to select a row, no matter which row I select, the data from the last loaded cell is always the data passed to the modalViewController.
I have logged to ensure the correct row is selected (it is) but no matter which row is selected, the last data loaded is always the data that is pushed.
First the Data Source Methods
#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSUInteger count = [self.results count];
return count > 0 ? count : 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ResultCellIdentifier = #"ResultCell";
static NSString *LoadCellIdentifier = #"LoadingCell";
NSUInteger count = [self.results count];
if ((count == 0) && (indexPath.row == 0)) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LoadCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:LoadCellIdentifier];
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
if (self.connection) {
cell.textLabel.text = #"Loading...";
} else {
cell.textLabel.text = #"Not available";
}
return cell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ResultCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:ResultCellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont systemFontOfSize:14.0];;
}
UIImage *image = [UIImage imageNamed:#"medicaltag.png"];
cell.imageView.image = image;
// Begin UITableCell Data Formatting
NSDictionary *tweet = [self.results objectAtIndex:indexPath.row];
NSString* tweetText = [tweet objectForKey:#"text"];
if ([tweetText rangeOfString:#" *** "].location !=NSNotFound) {
NSArray *textItems = [tweetText componentsSeparatedByString:#" *** "];
NSLog(#"%#", textItems);
callAddress = [textItems objectAtIndex:0];
callAddress = [callAddress stringByReplacingOccurrencesOfString:#" , " withString:#", "];
callType = [textItems objectAtIndex:1];
NSLog(#"%#", callType);
NSLog(#"%#", callAddress);
NSString *latitude = [textItems objectAtIndex:2];
NSString *latStringPt1 = [[NSString alloc] init];
NSString *latStringPt2 = [[NSString alloc] init];
NSString *longitude = [textItems objectAtIndex:3];
longitude = [longitude stringByReplacingOccurrencesOfString:#"- " withString:#"-"];
NSString *lonStringPt1 = [[NSString alloc] init];
NSString *lonStringPt2 = [[NSString alloc] init];
int latStringLen = [latitude length];
int lonStringLen = [longitude length];
NSLog(#"The value of integer num is %i", latStringLen);
latStringPt1 = [latitude substringWithRange:NSMakeRange(0,latStringLen-6)];
latStringPt2 = [latitude substringFromIndex:latStringLen-6];
combinedLatString = [latStringPt1 stringByAppendingString:#"."];
combinedLatString = [combinedLatString stringByAppendingString:latStringPt2];
lonStringPt1 = [longitude substringWithRange:NSMakeRange(0,lonStringLen-6)];
lonStringPt2 = [longitude substringFromIndex:lonStringLen-6];
combinedLonString = [lonStringPt1 stringByAppendingString:#"."];
combinedLonString = [combinedLonString stringByAppendingString:lonStringPt2];
NSLog(#"%#", combinedLatString);
NSLog(#"%#", combinedLonString);
}
cell.textLabel.text = [NSString stringWithFormat:#"%#", callAddress];
cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#", callType];
cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
return cell;
}
Now the Delegate Method
#pragma mark -
#pragma mark Table View Delegate Methods*
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSLog(#"%i", row);
CallMapViewController *mapVC = [[CallMapViewController alloc] initWithNibName:#"CallMapViewController" bundle:[NSBundle mainBundle]];
mapVC.annotCallType = callType;
mapVC.annotCallAddress = callAddress;
NSLog(#"%#", mapVC.annotCallType);
NSLog(#"%#", mapVC.annotCallAddress);
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *lat = [f numberFromString:combinedLatString];
NSNumber *lon = [f numberFromString:combinedLonString];
mapVC.annotLatCoord = lat;
mapVC.annotLonCoord = lon;
NSLog(#"%#", lat);
NSLog(#"%#", lon);
NSLog(#"%#", callType);
NSLog(#"%#", callAddress);
[self presentModalViewController:mapVC animated:YES];
}
You already have your tweet data stored in your viewController's property results, so you just need to grab the data from there and parse it again (as Daryl Teo mentions) in didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* tweetText = [[self.results objectAtIndex:indexPath.row] objectForKey:#"text"];
if ([tweetText rangeOfString:#" *** "].location != NSNotFound) {
NSArray *textItems = [tweetText componentsSeparatedByString:#" *** "];
CallMapViewController *mapVC = [[CallMapViewController alloc] initWithNibName:#"CallMapViewController" bundle:[NSBundle mainBundle]];
mapVC.callAddress = [[textItems objectAtIndex:0] stringByReplacingOccurrencesOfString:#" , " withString:#", "];
mapVC.callType = [textItems objectAtIndex:1];
[self presentModalViewController:mapVC animated:YES];
}
}
You get [indexPath row] but you don't use it.
And you've got "callType" and "callAddress" which aren't within the scope of the delegate method. They exist as instance variables of the viewController, and you set their values as you're creating the cells. That's why their values are always the values of the last cell.
You need to store the data in memory, so that you can reference it when you get the row from indexPath.
NSInteger row = [indexPath row];
NSString *tweetText = [tweetsStorageArray objectAtIndex: row];
/* Parse Tweet Text again */
You can either store tweets as text, or create a storage class and store that.
From what I can see in the method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
You take the [indexPath row]; but you don't chose your data base on that.
I see that in your delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath () you are only accessing the selected row variable for NSLog messages.
You would probably need to access the cell using UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath] and its contents using callType = cell.detailTextLabel.text;and callAddress = cell.textLabel.text;
My NSMutableArray seems to be releasing and I can't figure out where, here is my code:
- (void)awakeFromNib {
arrayOfData = [[NSMutableArray alloc] initWithCapacity:0];
[arrayOfData addObject:#"test"];
NSLog(#"array: %#", arrayOfData);
}
All is well here!
Then I create a UITable and add it to the view, then I request some data, everything is normal.
arrayOfData = [response objectForKey:#"results"];
NSLog(#"count: %i", [arrayOfData count]);
NSLog(#"success!");
numberOfRows = [arrayOfData count];
[self.myTableView reloadData];
Creating the number of rows works fine too:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"test: %i", numberOfRows);
return numberOfRows;
}
Now accessing my array gets a "message sent to deallocated instance" at that line.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
// crashes here!
NSLog(#"array: %i", [arrayOfData count]);
if (numberOfRows){
NSString *filename = [[arrayOfData objectAtIndex:indexPath.row] objectForKey:#"filename"];
NSString *url = [NSString stringWithFormat: #"http://www.web.com/dir/%#", filename];
[cell.imageView setImageWithURL:[NSURL URLWithString:url]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.textLabel.text = #"My Text";
}
return cell;
}
I have also have this in my header, and I used #synthesize as well
#property (nonatomic, retain) NSMutableArray *arrayOfData;
Any help would be appreciated!
It looks like you're reassigning arrayOfData with this line:
arrayOfData = [response objectForKey:#"results"];
If that's what you want, try this instead:
arrayOfData = [[response objectForKey:#"results"] retain];