Home > DeveloperSection > Forums > Accessing sub-view selection of UITableView in iOS.
Maria Susan

Total Post:29

Points:107
Posted on    January-07-2016 4:39 AM

 iPhone iPhone  Ios  Objective C 
Ratings:


 1 Reply(s)
 326  View(s)
Rate this:
I have created a view controller in iPhone app, which has a button. On button click we open a UITableViewController, on this table view we populate data using a mutable array and that array has several NSDictionary objects.
#import <UIKit/UIKit.h>
@interface MyTableViewController:UITableViewController
{
NSMutableArray *arrayList;
NSIndexPath *last;
IBOutlet UITableView *tableView;
NSObject *selectedItem;
}
@property(nonatomic, retain) NSArray *arrayList;
@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, retain) NSObject *selectedItem;

@end
this is the Implementation of MyTableViewController.m
#import "MyTableViewController.h"
@implementation MyTableViewController

@synthesize arrayList, tableView, selectedItem;
Now, I have no problem to open that table view on button click, but I can not figure-out what are the properties of the selected item.


Tarun Kumar

Total Post:213

Points:1635
Supported
Posted on    January-07-2016 7:16 AM

For accessing selection in UITableView you can use -indexPathForSelectedRow method to determine which row is selected, this is a predefined method of table view.
You can use this method like this:
NSIndexPath *selectedIndex = [myTableView indexPathForSelectedRow];
if(selectedIndex) {
   NSLog(@"Section No:%u and Row No:%u", [selectedIndex row],
[selectedIndex section]);
} else {
   NSLog(@"Please select a Row!");
}
The Log message will display in the console on selection changes.
and implement didSelectRowAtIndexPath: method, it is a delegate method of table view in your view controller like this:
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath
{
    NSLog(@"Row selection Changed");
}


Modified On Jan-07-2016 07:17:41 AM

Don't want to miss updates? Please click the below button!

Follow MindStick