KDaker
KDaker

Reputation: 5909

get value from custom UITableViewCell

I have a custom UITableViewCell that has a UISegmentedControl object. My UITableView (questionnaire form) has 6 of this custom cell (6 segmented controls). I am having trouble getting the selectedSegmentIndex of the segment controls. My guess is that the cells are being released after the table is generated. I am getting the values using the following code:

MyCustomCell *q1 = (MyCustomCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathWithIndex:0]];

int segmentIndex = (int) q1.segmentedController.selectedSegmentIndex;

the int segmentIndex is always giving the same value of 0 no matter what the selected index is.

Upvotes: 2

Views: 1622

Answers (1)

user756245
user756245

Reputation:

You must use the right method to initialize your index path for the section and row you want use, otherwise row and section properties won't be correctly set, and you will get the same cell each time :
+(NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section;.

The code looks like :

// getting the cell at third row of first section
NSIndexPath *ip = [NSIndexPath indexPathForRow:2 inSection:0];
MyCustomCell *q1 = (MyCustomCell *)[tableView cellForRowAtIndexPath:ip];
int segmentIndex = (int) q1.segmentedController.selectedSegmentIndex;

You could also consult : NSIndexPath UIKit Additions Reference for more information.

Upvotes: 4

Related Questions