【问题标题】:How to update UITableView datasource when user changed the value in UITableViewCell当用户更改 UITableViewCell 中的值时如何更新 UITableView 数据源
【发布时间】:2014-10-17 16:54:03
【问题描述】:

首先,这是一个供老师考勤的应用程序,考勤将更新回我的服务器。我的 TableViewCell 自定义如下。

@interface TvcStudentClassSession : UITableViewCell

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

@end

我在 TableViewCell 中为 SegmentedControl 创建了一个 IBAction。当它被选中时,它应该调用我的网络服务并立即在服务器中更新值。这个没有问题。

现在的问题是,我的 UITableViewCell 填充在我的父视图中。代码如下。 objStudentClassSessions 是一个 NSMutableArray,用于存储从服务器加载的值。

- (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;
}

每次用户更改 SegmentedControl 时,都会在 UiTableViewCell 类中成功调用 IBAction 并成功更新到服务器。但是一旦我向下滚动屏幕然后返回,SegmentedControl 就会恢复到原始状态,我知道这就是 iOS 的工作方式,它每次加载单元格值都变得可见。

所以对于我的情况,我可以知道如何在 UITableView 中更新我的 objStudentClassSessions 以便在单元格再次可见时正确重新加载?

【问题讨论】:

    标签: ios uitableview uisegmentedcontrol


    【解决方案1】:

    当段发生变化时,您还需要更新本地数据。所以在代码处理段发生了这样的变化:

    ObjStudentClassSession *objStudentClassSession = [objStudentClassSessions objectAtIndex:indexOfStudentFromSegmentedControl];
    objStudentClassSession.attendanceStatus == segentedControl.selectedSegmentIndex == 0 ? ...
    

    【讨论】:

    • 但是,我的 SegmentedControl 操作位于 UITableViewCell 类中。但是我的数据源驻留在父 UIView 中,其中包含使用 UITableViewCell 的 UITableView。如何将值传递回该类?
    • 还是我的建筑师设计错了?可能有更好的方法来代替将 SegmentedControl 操作放在 Parent UIView 类中?但是,我怎样才能获得触发该值更改操作的 UITableViewCell?
    • 你是否使用 [tableview ReloadData] 重新加载 tableView ?;
    • @teapeng 通常你在 UIViewController 中有数据源。要从没有自定义类的单元格内的分段控件中查找索引路径,请使用以下类别:stackoverflow.com/a/18813535/2754158
    • 我使用委托将所有值传递回父 UIViewController。任务完成。感谢您的建议。
    猜你喜欢
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 2012-02-17
    • 2017-05-27
    • 1970-01-01
    相关资源
    最近更新 更多