I am using below code for getting index number of the row. I want to get the Text of row. Please tell me, what modifications are required to get the text from each row.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath{
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"Detail"];
[self.navigationController pushViewController:detail animated:YES];
detail.rowNum.text = [NSString stringWithFormat:#"Row: %d", (indexPath.row) ];
}
thanx in advance :)
You can get cell with all its properties this way:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath{
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"Detail"];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
detail.rowNum.text = [NSString stringWithFormat:#"Row: %d", (indexPath.row) ];
detail.someLabel.text = cell.textLabel.text;
[self.navigationController pushViewController:detail animated:YES];
}
Is this what you want? Text from selected cell?
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath{
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"Detail"];
[self.navigationController pushViewController:detail animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = cell.textLabel.text;
detail.rowNum.text = cellText;
}
Update ur code as above.
Related
When a table view is pressed, I am trying to grab the value of a textview from that table view row that has been selected as follows.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
tabBar = [self.storyboard instantiateViewControllerWithIdentifier:#"TabBarController"];
[self.navigationController pushViewController:tabBar animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
MyOrdersController *detailVC = [[MyOrdersController alloc]init];
NSLog(#"hello %#", detailVC.shipmentReferenceNumberTextLabel.text);
}
But im getting null value. How can I get the value of the textview in that specific row?
You can grab the textview from selected cell subview.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
for (id txtView in selectedCell.subviews)
{
if ([txtView isKindOfClass:[UITextView class]])
{
UITextView* tv = (UITextView*)txtView;
NSString* text = tv.text;
}
}
}
Just try it:-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = selectedCell.textLabel.text;
}
Im using the following code to get the index of tablecell that got clicked.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
tabBar = [self.storyboard instantiateViewControllerWithIdentifier:#"TabBarController"];
[self.navigationController pushViewController:tabBar animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[tableView setAllowsSelection:YES];
NSLog(#"you have selected %d",indexPath.row);
}
The problem with this code is that it prints the index value of row only once. If I tap the tableviews anymore, it doesn't print the index value. How to know which index I have clicked ?
try this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSLog(#"you have selected %d",indexPath.row);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[tableView setAllowsSelection:YES];
tabBar = [self.storyboard instantiateViewControllerWithIdentifier:#"TabBarController"];
[self.navigationController pushViewController:tabBar animated:YES];
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell * tableCell = [tableView cellForRowAtIndexPath:indexPath];
BOOL isSelected = (tableCell.accessoryType == UITableViewCellAccessoryCheckmark);
if (isSelected)
{
tableCell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
tableCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
Now check when accessory check marks appears it will print the index path or not??
What I am trying to do is moving from one table view controller to another. I am sending some data and the data is passed but id does not get updated in the table view. That is, when i click on one of the options in the table view, it shows the same screen again. Here is the code snippet I am using to push the table view:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
devicedetailTableViewController *detailViewController = [[devicedetailTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
// NSLog(#"%#",detailViewController.details);
[self.navigationController pushViewController:detailViewController animated:YES];
detailViewController.details = self.data[indexPath.row];
}
Please help! Please note: I am not using storyboard here because the previous screen is a login screen ad segues are causing a problem for me there. So I decided to switch to code only. So, I cannot use storyboard.
PS: I know that the data is getting passed because i can see it in my log.
This is how I am loading data:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView registerNib:[UINib nibWithNibName:#"nibname"
bundle:nil]
forCellReuseIdentifier:#"Cell"];
//static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (!cell)
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
switch (indexPath.row) {
case 0:
cell.textLabel.text = [self devices];
cell.detailTextLabel.text = #"Name";
break;
case 1: {
NSString *battlife = [details objectForKey:#"battlife"];
cell.textLabel.text = battlife;
cell.detailTextLabel.text = #"Battery Life";
break;
}
case 2:{
cell.textLabel.text = [details objectForKey:#"location"];
cell.detailTextLabel.text = #"Location";
break;}
default:
break;
}
return cell;
}
you are pushing the view before setting the data,
[self.navigationController pushViewController:detailViewController animated:YES];
detailViewController.details = self.data[indexPath.row];
do first the data set then push
detailViewController.details = self.data[indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
also you might need to do [table reloadData] depending on how u load the data on the second VC
I have a table view which pushes a detailed view on cell click. The new view pushes however I'm trying to pass a web address to the detailed view which loads a webView but I'm unsure on how to do this. Here is my code:
-(void)setupArray {
webAddress = [[NSMutableDictionary alloc]init];
[webAdress setObject:#"http://www.google.com.au" forKey:#"first item"];
[webAdress setObject:#"http://www.yahoo.com.au" forKey:#"second item"];
menuItems = [webAdress allKeys];
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [webAdress count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [menuItems objectAtIndex:indexPath.row];
return cell;
}
- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:#"detail"];
[self.navigationController pushViewController:detailView animated:YES];
}
Any help would be greatly appreciated.
You need to use a property for that in your detail view and pass the URL in the didSelectRowAtIndexPath method like this :
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:#"detail"];
detailView.webAddress = [menuItems objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailView animated:YES];
}
and in your DetailView :
#property (nonatomic, strong) NSString *webAddress;
You should add an instance variable to the DetailViewController which will hold the address & set it to the desired value. Then (with the proper accessor) set it before/after pushing the view.
e.g. if you defined the variable as webAddress:
DetailViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:#"detail"];
[detailView setWebAddress:[webAddress objectForKey:[menuItems objectAtIndex:[indexPath row]]]]; // or some other way to get the correct address
[self.navigationController pushViewController:detailView animated:YES];
The only thing that remains to be done is to set the web view's address to this value on viewWillAppear: or something.
Edit:
A sample viewWillAppear: method would look like this:
- (void)viewWillAppear:(BOOL)animated
{
// construct request
NSURL *url = [NSURL URLWithString:webAddress];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];
[super viewWillAppear:animated];
}
I have an app that is parses XML and stores in an NSObject ("Call"). I then display the table in a data like so:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Call *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = currentCall.call_name;
cell.detailTextLabel.text = currentCall.location;
cell.accessoryType = UIButtonTypeRoundedRect;
return cell;
}
My question is when I am pushing to the detail view controller, how do I set up the didSelectRowAtIndexPAth method to be able to access all the objects properties? I use to use this way, which works, but I am sure it is not the most efficient way. Thanks for the help.
Old way:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"indexPath = %d", indexPath.row);
Call *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row / 2];
self.currentCallTypeDetail = currentCall.currentCallType;
self.unitsDetail = currentCall.units;
self.locationDetail = currentCall.location;
self.countyDetail = currentCall.county;
self.stationDetail = currentCall.station;
self.fullcallnumberDetail = currentCall.fullcallnumber;
self.latitude = currentCall.latitude;
self.longitude = currentCall.longitude;
self.callTypeDetail = currentCall.callType;
self.callNumberDetail = currentCall.callnumber;
self.timeDetail = currentCall.time;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
CallTypeDetailController *vc = [storyboard instantiateViewControllerWithIdentifier:#"calltypedetail"];
[vc setCurrentCallTypeDetail:self.currentCallTypeDetail];
[vc setUnitsDetail:self.unitsDetail];
[vc setLocationDetail:self.locationDetail];
[vc setCountyDetail:self.countyDetail];
[vc setStationDetail:self.stationDetail];
[vc setFullcallnumberDetail:self.fullcallnumberDetail];
[vc setCallTypeDetail:self.callTypeDetail];
[vc setCallNumberDetail:self.callNumberDetail];
[vc setTimeDetail:self.timeDetail];
[vc setLatitude:self.latitude];
[vc setLongitude:self.longitude];
[self.navigationController pushViewController:vc animated:YES];
NSLog(#"Selected Call = %#", self.currentCallTypeDetail);
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"indexPath = %d", indexPath.row);
Call *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row / 2];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
CallTypeDetailController *vc = [storyboard instantiateViewControllerWithIdentifier:#"calltypedetail"];
[vc setCurrentCall:currentCall];
[self.navigationController pushViewController:vc animated:YES];
NSLog(#"Selected Call = %#", self.currentCallTypeDetail);
}