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?