3

我目前正在制作一个显示用户个人资料的应用程序。为此,我为不同类型的数据(电话号码、邮件地址等)使用了带有自定义单元格的 UITableViewCell。每个配置文件最多有 8 个单元。

允许用户以最简单的方式编辑其个人资料。当 tableview 的编辑模式被触发时,所有可编辑的标签都被 textfields 替换。然后在修改完成后返回标签。

Homever,不可见的单元格似乎有问题。每次它们重新出现在视图中时,它们都会被重新加载,再次触发 setEditing:YES 方法等等......因此,在文本字段中所做的每一次更改都会丢失。

有没有办法防止 tableview 删除不可见的单元格并将它们添加回来?只有八个单元格,因此不会占用太多资源,而且每次进行更改时我都不必保存它们的状态。

PS:我用 dequeueReusableCellWithIdentifier 方法和每个单元格的标识符尝试了几件事,但我没有设法实现我想要的。每次我隐藏一个单元格时,它的内容都会刷新。

4

2 回答 2

3

您应该使用静态单元格而不是动态单元格。选择表格视图并更改配置,如图像。 在此处输入图像描述

并在界面生成器中添加单元格!

于 2014-07-27T10:56:46.313 回答
1

在这种情况下,UITableView 的可重用性对您没有帮助(在大多数情况下,可重用性当然是一件好事),但在保留编辑时会遇到太多困难。因此,您可以避免重复使用并提前准备好您的细胞。

在 ViewController 中添加NSMutableArrayiVar 或属性

@property (nonatomic, strong) NSMutableArray *cells;

在你看来DidLoad:准备你的cells没有任何reuseIdentifier

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    //Creates tableView cells.
    [self createCells];

}

- (void)createCells
{
    self.cells = [NSMutableArray array];

    TCTimeCell *cellCallTime = [[TCTimeCell alloc] initWithTitle:@"CALL" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeCall];
    [_cells addObject:cellCallTime];

    TCTimeCell *cellLunchOut = [[TCTimeCell alloc] initWithTitle:@"LUNCH START" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeLunchOut];
    [_cells addObject:cellLunchOut];

    TCTimeCell *cellLunchIn = [[TCTimeCell alloc] initWithTitle:@"LUNCH END" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeLunchIn];
    [_cells addObject:cellLunchIn];

    TCTimeCell *cellSecondMealOut = [[TCTimeCell alloc] initWithTitle:@"2ND MEAL START" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeSecondMealOut];
    [_cells addObject:cellSecondMealOut];

    TCTimeCell *cellSecondMealIn = [[TCTimeCell alloc] initWithTitle:@"2ND MEAL END" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeSecondMealIn];
    [_cells addObject:cellSecondMealIn];

    TCTimeCell *cellWrapTime = [[TCTimeCell alloc] initWithTitle:@"WRAP" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeWrap];
    [_cells addObject:cellWrapTime];
}

您可以从此数组中填充您的 tableView。

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cells.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.cells[indexPath.row];
}

如果你有一个分段的 tableView,你可以将你的单元格准备为array of arrays. 在这种情况下,您的数据源方法应如下所示

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.cells count];
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.cells[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.cells[indexPath.section][indexPath.row];
}
于 2014-07-27T11:02:46.177 回答