Xcode show UIAlertView after UITableRowSelection? - ios

this is my first question, sorry if something is wrong
Well I'm trying to create a view in which i can select a friend from a table view and then it should say the number and the mail on a UIAlertView but I dont know how to do this
the friend list is obtained from an xml file on my site, and then is parsed an showed on a table with a custom cell design
this is the code that creates each cell
- (UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[self.messageList dequeueReusableCellWithIdentifier:#"ContactListItem"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ContactListItem" owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
}
NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row];
UILabel *userLabel = (UILabel *)[cell viewWithTag:2];
userLabel.text = [itemAtIndex objectForKey:#"user"];
return cell;
}
thanks
Santiago

You have to implement the didSelectRowAtIndexPath: method.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row];
NSString *name = [itemAtIndex objectForKey:#"user"];
NSString *email = [itemAtIndex objectForKey:#"email"];
NSString *phone = [itemAtIndex objectForKey:#"phone"];
NSString *messageStr = [NSString stringWithFormat:#"Email : %#\nPhone : %#", email, phone];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:name message:messageStr delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath is the method you would need to implement. This is assuming the view you are presenting the table view in is the delegate for the table view.
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Alert view logic happens here getting all the cell information
}

Related

Why same nsmutablearray gives me valid value and a null on different functions used

I have a NSMutableArray with some objects of type Notes i.e. my class with attributes, iD,note,noteTitle.. I am using the notes array to populate a tableview, and on click, I am trying to open another controller view, to show that specific table row clicked
My code are :
when controller load:
- (void)viewDidLoad {
[super viewDidLoad];
Notes * myNotes =[[Notes alloc] init];
notes = [myNotes getMyNotes];
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSString *title = [NSString stringWithFormat:#"%#...",((Notes *) [notes objectAtIndex:indexPath.row]).noteTitle ];
// here i am using my notes nsmutablearray from above method to populate tableview list of titles.. and it is populated fine.
cell.textLabel.text = title;
cell.imageView.image=[UIImage imageNamed:#"back.jpg"];
return cell;
}
Now when I click a row, I am trying to just see if, I will be getting title, body and it for that certain note..
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
long selectedRow = indexPath.row;
NSString *title = [NSString stringWithFormat:#"%#...",((Notes *) [notes objectAtIndex:selectedRow]).notes];
NSLog(#"%#",title);
}
But I am getting null this time...
why same code in above function is populating my table view but here not even logging it.
Thank you in advance....
You can try in didSelectRowAtIndexPath method
Notes *note = [[Notes alloc]init];
note = [notes objectAtIndex: indexPath.row];
NSString *title = [NSString stringWithFormat:#"%#...",note.notes];
NSLog(#"%#",title);
Hope it works.

How to send value to anthor Page from table view iOS [duplicate]

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 8 years ago.
I have done for parsing data from JSON and put it show in Table view.
So I want to pass data (catID and catName )to another Page it's DetailPageController
how can I do like that? the below is my code.
Thanks in advance!
#import "ViewController.h"
#interface ViewController ()
{
NSMutableArray *myObject;
// A dictionary object
NSDictionary *dictionary;
// Define keys
NSString *catName;
NSString *catID;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
catName =#"Category Name";
catID=#"Category ID";
myObject = [[NSMutableArray alloc] init];
NSData *jsonSource = [NSData dataWithContentsOfURL:
[NSURL URLWithString:#"MY_URL"]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:
jsonSource options:NSJSONReadingMutableContainers error:nil];
for (NSDictionary *dataDict in jsonObjects) {
NSString *TheCatName = [dataDict objectForKey:#"cat_name"];
NSString *TheCatID = [dataDict objectForKey:#"cat_id"];
NSLog(#"cat_name: %#",TheCatName);
NSLog(#"cat_id: %#",TheCatID);
dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
TheCatName, catName,
TheCatID, catID,
nil];
[myObject addObject:dictionary];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return myObject.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Item";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell=[[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
NSMutableString *textcat;
textcat = [NSMutableString stringWithFormat:#"%#",
[tmpDict objectForKeyedSubscript:catName]];
NSMutableString *catid;
catid = [NSMutableString stringWithFormat:#"ID: %# ",
[tmpDict objectForKey:catID]];
cell.textLabel.text = textcat;
cell.detailTextLabel.text= catid;
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
NSMutableString *catID;
catID = [NSMutableString stringWithFormat:#"%#",
[tmpDict objectForKeyedSubscript:cat_id]];
NSLog(#"didSelectRowAtIndexPath");
/*UIAlertView *messageAlert = [[UIAlertView alloc]
initWithTitle:#"Row Selected" message:#"You've selected a row" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];*/
UIAlertView *messageAlert = [[UIAlertView alloc]
initWithTitle:#"Row Selected" message:catID delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
// Display the Hello World Message
[messageAlert show];
// Checked the selected row
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
First import DetailPageController in TableviewController Class with help of following line.
#import "DetailPageController.h"
Now implement following method in your TableViewController Class
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailPageController * detailPageControllerOBJ_ = [[ImageOptionViewController alloc]initWithNibName:#“XIBName bundle:nil];
detailPageControllerOBJ_.dicSelectedData = [self.arrayData objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailPageControllerOBJ_ animated:YES];
}
And in DetailPageController.h put following code.
#property (nonatomic,strong) NSDictionary * dicSelectedData;
You will get your selected data in dicSelectedData Dictionary

UiTableview values not getting update upon the array value changed

I am fetching data from a web service which returns an array and I am displaying that array in the table view. I am facing the issue that table values are getting updated but after some time as first it is showing the old value and upon scrolling it shows the new value.
Below is my code.
NSURLRequest *request=[NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:8.0];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSMutableDictionary *ResultdicNew = [[NSMutableDictionary alloc]initWithDictionary:(NSMutableDictionary *)responseObject];
NSString *message=[ResultdicNew objectForKey:#"status"];
NSString *message1=#"";
if([message isEqualToString:#"success"])
{
NSMutableDictionary *datadic=[ResultdicNew objectForKey:#"data"];
NSMutableDictionary *listDataDic=[datadic objectForKey:#"competitors"];
affArray=[listDataDic valueForKey:#"affiliation"];
countArray=[listDataDic valueForKey:#"total_count"];
_lblEventTitle.text=[datadic valueForKey:#"group_name"];
NSLog(#"affArray here is %#",affArray);
NSLog(#"countArray.count here is %ld",countArray.count);
[self revealCompetitorsView];
}
else
{
message1=[Resultdic objectForKey:#"data"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:message1
message:message
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
}
[SVProgressHUD dismiss];
// NSLog(#"JSON Here is :%lu",(unsigned long)Resultdic.count);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[SVProgressHUD dismiss];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error Retrieving Data"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
}];
[operation start];
#pragma mark - UITableView Datasource, Delegate Methods
#pragma mark -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [countArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
static NSString *CellIdentifier = #"menuCellNew";
cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"RAMenuCellNew" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// [cell SetData:[m_ary objectAtIndex:indexPath.row]];
UILabel *lbl=(UILabel*)[cell viewWithTag:420];
UILabel *lbl1=(UILabel*)[cell viewWithTag:421];
lbl.text=[affArray objectAtIndex:indexPath.row];
lbl1.text=[countArray objectAtIndex:indexPath.row];
}
[self.sideTable reloadData];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%ld",(long)indexPath.row);
}
Your problem is in your cellForRowAtIndexPath - you are not updating the cell when you are given an existing cell for reuse.
You need to move the updating of the cell fields outside of the if (cell==nil)... block -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
static NSString *CellIdentifier = #"menuCellNew";
cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"RAMenuCellNew" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// [cell SetData:[m_ary objectAtIndex:indexPath.row]];
UILabel *lbl=(UILabel*)[cell viewWithTag:420];
UILabel *lbl1=(UILabel*)[cell viewWithTag:421];
lbl.text=[affArray objectAtIndex:indexPath.row];
lbl1.text=[countArray objectAtIndex:indexPath.row];
return cell;
}
Also, you can register your nib file for your cell in viewDidLoad and then you can use dequeueReusableCellWithIdentifer:forIndexPath which will automatically allocate a new cell if required and you can skip the whole if (cell==nil... bit.
You should also consider creating a UITableViewCell subclass and then you can assign your text fields to IBOutlet properties instead of searching by tag.
Replace this code on cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
static NSString *CellIdentifier = #"menuCellNew";
cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"RAMenuCellNew" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// [cell SetData:[m_ary objectAtIndex:indexPath.row]];
UILabel *lbl=(UILabel*)[cell viewWithTag:420];
UILabel *lbl1=(UILabel*)[cell viewWithTag:421];
lbl.text=[affArray objectAtIndex:indexPath.row];
lbl1.text=[countArray objectAtIndex:indexPath.row];
return cell;
}

Change CellForRow after didSelectRow AKA calling cellForRowAtIndexPath programmatically

Im currently working on my first "big" project, a currency converter. When i select a row i want every cell to change value of "nan" label to the currency returned from the xmlparse class. But, i don't know how i should call this piece of code from inside the didSelectRow method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"doingit");
static NSString *simpleTableIdentifier = #"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
XMLparse *parse = [[XMLparse alloc]init];
[parse setcurrencyname:[Datarray2 objectAtIndex:indexPath.row]];
cell.nameLabel.text = [Datarray2 objectAtIndex:indexPath.row];
cell.prepTimeLabel.text = [NSString stringWithFormat:#"%.9f",[parse convertionrate]];
cell.thumbnailImageView.image = [UIImage imageNamed:[imagearray objectAtIndex:indexPath.row]];
return cell;
}
//didselectrow
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"didSelectRowAtIndexPath");
XMLparse *parse = [[XMLparse alloc]init];
[parse setHowmuchmoney:1];
[parse setValutanamn1:[Datarray objectAtIndex:indexPath.row]];
NSLog(#"%#",[parse valutanamn1]);
[parse loadDataFromXML];
UIAlertView *messageAlert = [[UIAlertView alloc]
initWithTitle:#"Row Selected" message:[Datarray objectAtIndex:indexPath.row] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[messageAlert show];
//call cellForRowAtIndexPath here?
}
You only have to do this:
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];

NSUserDefault not loading in table view

I have a main view with a button that when pressed it adds the url from the web view(like a bookmark) to a table view. The only problem is that its not loading. ive been at it for hours now but cant figure it out. I think i am loading the data in the table view wrong but not quiet sure.
This is the MainViewController.m
- (IBAction)bookmark:(id)sender {
UIActionSheet *actionSheet =
[[UIActionSheet alloc] initWithTitle:[[[[self webView] request] URL]
absoluteString] delegate:nil cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil otherButtonTitles:#"Add", nil];
[actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSMutableArray *bookmarks = [[[NSUserDefaults standardUserDefaults]
arrayForKey:#"Bookmarks"] mutableCopy];
if (!bookmarks) {
bookmarks = [[NSMutableArray alloc] init];
}
[bookmarks addObject:[[[[self webView]request] URL] absoluteString]];
[[NSUserDefaults standardUserDefaults] setObject:bookmarks
forKey:#"Bookmarks"];
}
}
This is BookmarksViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"%#", bookmarks);
bookmarks = [[[NSUserDefaults standardUserDefaults]
arrayForKey:#"Bookmarks"] mutableCopy];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[[NSUserDefaults standardUserDefaults] setObject:bookmarks forKey:#"Bookmarks"];
cell.textLabel.text = [self.bookmarks objectAtIndex:indexPath.row];
return cell;
}
When your app is first run there is nothing set for the Bookmarks key in NSUserDefaults. So the line:
bookmarks = [[[NSUserDefaults standardUserDefaults]
arrayForKey:#"Bookmarks"] mutableCopy];
in viewDidLoad leaves bookmarks set to nil.
In your action sheet handler you actually deal with this properly.
And one other big problem is the call to:
[[NSUserDefaults standardUserDefaults] setObject:bookmarks forKey:#"Bookmarks"];
in your cellForRowAtIndexPath... method. Why do you update NSUserDefaults every time a cell is needed? Don't do this.
The command [tableView dequeueReusableCellWithIdentifier:CellIdentifier] does NOT always return a cell. It only returns a cell if there is a cell in the reusable-cell queue.
So your method should be:
- (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];
}
[[NSUserDefaults standardUserDefaults] setObject:bookmarks forKey:#"Bookmarks"];
cell.textLabel.text = [self.bookmarks objectAtIndex:indexPath.row];
return cell;
}

Resources