didSelectRowAtIndexPath:(NSIndexPath *)indexPath returns wrong index - ios

my problem is that when i am clicking in the table view it returns this error
2013-02-22 11:22:42.293 Exercise[1570:c07] -[NSIndexPath item]: unrecognized selector sent to instance 0x6e7ddd0
2013-02-22 11:22:42.294 Exercise[1570:c07] *
and this is where it returns the wrong index
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"uitable index: %ld",(long)indexPath.item);
DBHelper *dataObj = [appDelegate.dataArray objectAtIndex:indexPath.row];
self.selIndex = indexPath.item;
LblID = dataObj.dataID;
VCFname.text = dataObj.DBFirstName;
VCLname.text = dataObj.DBLastName;
VCMobile.text = dataObj.DBMobile;
VCEmail.text = dataObj.DBEmail;
VCBday.text = dataObj.DBBirthdate;
NSLog(#"dataID:%i dataArrayCount:%i",self.selIndex,appDelegate.dataArray.count);
}
i have connected the table view's delegate and datasource to file owner.
EDIT: additional info
I am trying to get the data from my sqlite database to show on the textfield when i click in the tableview

That's not how NSIndexPath works. The index path has a 'row' and 'section' property, which you can use to determine the cell tapped. Here's the documentation for NSIndexPath.

the item property is declared In UICollectionView.h not in the UITableView.h. You can see item. And in the tableView , you should use the indexPath.section

Related

Save each didSelectRowAtIndexPath click's object in MutableArray

I have a list of items/cells in TableView. Table is set to allow multiple selection.
So, i have these methods to select and deselect.
#interface NotificationsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate,UIScrollViewDelegate>
{
NSMutableArray *checkedIndexPaths;
}
-(void)viewDidLoad{
//Setup default array
checkedIndexPaths = [[NSMutableArray alloc]init];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NotificationsTableViewCell *cell = (NotificationsTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.imgIcon.image=[UIImage imageNamed:#"select-icon"];
Notifications* selected = notifications[indexPath.section];
selectedGroup = selected.NotificationsId;
[checkedIndexPaths addObject:selectedGroup];
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NotificationsTableViewCell *cell = (NotificationsTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.imgIcon.image=[UIImage imageNamed:#""];
Notifications* selected = notifications[indexPath.section];
selectedGroup = selected.NotificationsId;
[checkedIndexPaths addObject:selectedGroup];
}
I want to add each object into array during multiple selection. But with this current coding, it will overwrite the same one, and i have no idea how can i make it multiple object array to store in each click of cell. Thank you for your helps.
You're creating new NSMutableArrays after each method call. Your listArray should be a property of the object, or otherwise visible outside of the scope of these two methods, that you add and remove items from on each method call.

how to use the function dequeueReusableCellWithIdentifier:forIndexPath?

I know that dequeueReusableCellWithIdentifier:forIndexPath is called by tableView method inside the tableviewcontroller. And if I understand correctly, tableView method is called several times until all the cells are populated. But what I don't know is where do you get the value for the parameter IndexPath? I want to use dequeueReusableCellWithIdentifier:forIndexPath for a method that I created because I want to access my cell to copy some values of its properties.
NOTE:
I already populated my cell, which means that I successfully used the method tableView.
(Edit) ADDITIONAL INFO:
I'm trying to create a profile and edit profile tableviews. Inside the profile tableview, I displayed the name, address, contact#, etc., of the user. Also, I have a segue called edit profile. In the edit profile, I have textfields for each category (name, address, etc.). What I want to do is, if I edit the contents of the textfields, I should be able to display the new contents in my profile tableview. An example case would be: in the profile view I'm displaying -> name:human, address:earth (each in its own cell). Now if I go to editprofile tableview, I will edit the contents such that -> name:alien, address:mars. After that, there is a button called 'apply' to end editing of contents and go back to profile tableview. If I go back to profile view, the display should now be name:alien, address:mars and not name:human, address:earth.
Here is some code if it's any help. The code is called by a button in tableviewcontroller. "MyCell" is the class of my cell. This code is not working properly. I hope someone can help me fix this.
- (IBAction)updateCopies:(id)sender {
static NSString *ident = #"MyCell";
NSIndexPath *indexPath;
//create cell
MyCell *cell = [self.tableView dequeueReusableCellWithIdentifier:ident forIndexPath:indexPath];
//create variable for accessing cells
int row = [indexPath row];
_labelValues[row] = cell.textField.text
}
You should only use dequeueReusableCellWithIdentifier when you need to supply the table view with a cell to display. If you want to get the UITableViewCell object at a certain index, you should use cellForRowAtIndexPath.
Your problem
What you really need is a model class. You can then pass this to the edit controller, which changes the properties. Then when you return to the tableView, you can reload it and display the new properties.
What you could also do is create a delegate protocol for your edit profile controller, something like EditProfileViewControllerDelegate with something like:
protocol EditProfileViewControllerDelegate {
- (void)editProfileViewController:(EditProfileViewController *)controller didUpdateName:(NSString *)name address:(NSString *)address;
}
You can implement this delegate in your table view controller and use it to update the values when the text is changed. However, this quickly becomes unwieldy, I would not recommend it over using a proper model class.
You can get indexPath using CGPoint..You can use dequeueResusableCell for reusability of the cell..
- (IBAction)updateCopies:(id)sender {
CGPoint position = [sender convertPoint:CGPointZero
toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:position];
//create variable for accessing cells
MyCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
int row = [indexPath row];
_labelValues[row] = cell.textField.text
}
Hope it helps you..
Use this
- (IBAction)updateCopies:(id)sender {
MyCell *parentCell = (MyCell *)sender.superview;
while (![parentCell isKindOfClass:[MyCell class]]) { // iOS 7 onwards the table cell hierachy has changed.
parentCell = parentCell.superview;
}
UIView *parentView = parentCell.superview;
while (![parentView isKindOfClass:[UITableView class]]) { // iOS 7 onwards the table cell hierachy has changed.
parentView = parentView.superview;
}
UITableView *tableView = (UITableView *)parentView;
NSIndexPath *indexPath = [tableView indexPathForCell:(MyCell *)parentCell];
NSLog(#"indexPath = %#", indexPath);
}
Well I got what you want to accomplish.
Firstly, there is a delegate which is being called when you click/select a cell and go to the Edit Profile page. That delegate is
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
///
}
Make a global variable, say selectedIndexPath which holds the current cell index path which is being edited. Update this value each time when you go to edit profile page.
Like this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedIndexPath = indexPath;
// code to go to edit page...
}
Now in your updateCopies Method, do like this
- (IBAction)updateCopies:(id)sender {
//get the existing cell with the indexPath
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:#[selectedIndexPath]];
[self.tableView endUpdates];
//rest of your code goes here...
}

UITableViewCell Data should load to other ViewController

I am using TableViewCell in my application. and the cell data like label text and image from imageview at perticular cell. should view in next view controller.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MainBodyViewController * mainViewController =[[MainBodyViewController alloc]init];
MasterDetails * masted =(MasterDetails*)[listArray objectAtIndex:indexPath.row];
NSLog(#"did select row at index is givne as:%#",masted);
mainViewController.headLbl.text=[NSString stringWithFormat:#"%#",masted.title];
}
But the problem is I am not getting data from listArray. The data coming from json.
You need to check
Table view No of rows should be [listarry count] to avoid out of
index.
"MasterDetails" object is created properly, as per no of cell == json parsed object.
"listarray" is allocated.

How to understand which UITableViewCell has been selected

I have a list of NSStrings stored in an NSMutableArray, each value in the NSMutableArray has been assigned to a UITableViewCell. Using the following method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
How do I figure out which cell was selected. I'm looking for some kind of value through NSIndexPath or any other method to change the icon of a UITableViewCell when it is selected. After I know which cell has been selected, I'd reassign the cell to the new image, and then reload the data using: [self.homeworkTable reloadData].
I'm looking for some kind of simple return value, like 0 if the first cell is selected, etc. I've tried utilizing the NSIndexPath with no luck, as an NSIndexPath is not an NSString. How would I go about doing this?
EDIT
I understand the answer may already be on Stack Overflow, I simply haven't found it, and redirecting me to the answer would be very much appreciated.
You can get it directly didSelectRowAtIndexPath (that's the whole point!) using indexPath.row
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
int index = indexPath.row;
//....
It will match the index of your datasource array.
Sometimes you will need to get it from outside didSelectRowAtIndexPath deleguate method, you can do it the following way:
NSIndexPath *indexPath = [self.homeworkTable indexPathForSelectedRow];
int index = indexPath.row;
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
You should use indexPath.row, this gets you the row that you selected in this tableview.
You should also check out the documentation on NSIndexPath:
https://developer.apple.com/library/Mac/documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/Reference/Reference.html

How to get custom cell data when row is tapped?

I have a custom table view cell with 3 fields in it. I need to get the data in one of those fields to use in a SQLite query.
I've looked at cellForRowAtIndexPath, but don't see how to address the particular cell I want (it was defined with an IBOutlet, so it has a name) and get it's value.
When the row is tapped, your didSelectRowAtIndexPath delegate method will be called. In that method, you can use [tableView cellForRowAtIndexPath:indexPath] to get the row's cell. Then you can get whatever you need out of the cell.
I figured it out... here is the code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SingletonClass *shareInstance= [SingletonClass sharedInstance];
sArray *sa = [shareInstance.listOfSites objectAtIndex:indexPath.row]; // (rows are zero based)
NSString *labelContent = sa.sSiteID; // labelContent = site_id
// get list of sites based on selected row
slSQLite *dbCode = [[slSQLite alloc] init];
[dbCode getListOfSites:labelContent]; // labelContent is used to build query
}

Resources