0

Sample Screen

First and foremost, this is an app for teacher to make attendance, the attendance will update back to my server. My TableViewCell is customised as below.

@interface TvcStudentClassSession : UITableViewCell

@property (strong, nonatomic) IBOutlet UILabel *lblStudentInfo;
@property (strong, nonatomic) IBOutlet UISegmentedControl *smgStatus;
@property NSInteger studentId;

@end

I have created a IBAction for the SegmentedControl within the TableViewCell. When it is selected, it should call my webservice and update the value immediately in the server. There is no problem on this.

The problem right now is, my UITableViewCell was populated in my parent View. The code is as below. The objStudentClassSessions is a NSMutableArray that stored values loaded from the server.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"tvcStudentClassSession";

    TvcStudentClassSession *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    ObjStudentClassSession *objStudentClassSession = [objStudentClassSessions objectAtIndex:indexPath.row];

    cell.classSessionId = selectedObjClassSession.classSessionId;
    cell.studentId = objStudentClassSession.studentId;
    cell.lblStudentInfo.text = [NSString stringWithFormat:@"%@ (%@)", objStudentClassSession.studentName, objStudentClassSession.studentCode];

    if([objStudentClassSession.attendanceStatus isEqualToString:STATUS_NONE])
    {
        [cell.smgStatus setSelectedSegmentIndex:0];
    }
    else if([objStudentClassSession.attendanceStatus isEqualToString:STATUS_PRESENT])
    {
        [cell.smgStatus setSelectedSegmentIndex:1];
    }
    else if([objStudentClassSession.attendanceStatus isEqualToString:STATUS_ABSENT])
    {
        [cell.smgStatus setSelectedSegmentIndex:2];
    }

    return cell;
}

Every time when user changes the SegmentedControl, the IBAction called successfully within the UiTableViewCell class and updated to server successfully. But once I scroll down the screen and then back up, the SegmentedControl return to the original state, I know this is how iOS is working, it load the cell value every time is become visible.

So for my case, may I know how can I update my objStudentClassSessions in the UITableView so that it is correctly reload when the cell become visible again?

TPG
  • 2,811
  • 1
  • 31
  • 52

1 Answers1

1

You also need to update the local data when segment changes. So something like this in the code handling segment change:

ObjStudentClassSession *objStudentClassSession = [objStudentClassSessions objectAtIndex:indexOfStudentFromSegmentedControl];
objStudentClassSession.attendanceStatus == segentedControl.selectedSegmentIndex == 0 ? ...
Krzysztof
  • 1,441
  • 11
  • 22
  • However, my SegmentedControl action is inside the UITableViewCell class. But my datasource is residing in the parent UIView which contains my UITableView which using the UITableViewCell. How can I pass the value back to that class? – TPG Oct 17 '14 at 09:25
  • Or my architect has designed wrongly? Probably there is a better way of doing thing like put the SegmentedControl action in the Parent UIView class instead? But then how can I get the UITableViewCell that triggered that Value Changed action? – TPG Oct 17 '14 at 09:26
  • Did you use [tableview ReloadData] for reload tableView ?; – Rajpal Thakur Oct 17 '14 at 10:05
  • @teapeng normally you have datasource in UIViewController. To find the index path from segmented control inside cell without custom class use the following category: http://stackoverflow.com/a/18813535/2754158. – Krzysztof Oct 17 '14 at 10:40
  • I used a delegate to pass all the value back to the parent UIViewController. Mission done. Thanks for the advise. – TPG Oct 17 '14 at 11:32