Check progress where is necessary UITableView
我在保存 UITableViewCell 的状态时遇到问题,不知道如何解决。希望有人可以帮助我。
解释:
服务器上有一个 API,我从中获取数据,然后将其存储在 NSMutableArray 中。数组的每个对象都包含属性 ready,它可以是 1 或 0。所以我用这个数据填充 UITableView 没有问题,但不是每个数据对象都准备好了(即 0),我需要在服务器上获得完成进度,然后在每个单元格中显示它需要它。我在 UITableViewCell 的动态原型中有 UIProgressView 并在获取后设置进度。如果这样的”未准备好”对象只有一个,则没有问题。但是如果有很多对象,我无法显示进度,我不明白为什么。
这是我的代码。
cellForRowAtIndexPath 方法:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ static NSString *CellIdentifier = @“readyCell”; AVMMovieCell *cell = [self.readyTable dequeueReusableCellWithIdentifier:CellIdentifier]; // Configure the cell… if (cell == nil) { cell = (AVMMovieCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; } AVMFilmsStore *oneItem; oneItem = [readyArray objectAtIndex:indexPath.row]; NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:(unsigned int)indexPath.row]; if (oneItem.ready==1){ if ([selecedCellsArray containsObject:[NSString stringWithFormat:@“%@”,rowNsNum]] ) if (![cell.progressLabel.text isEqualToString:@“”]&& ![cell.progressLabel.text isEqualToString:@“Success”] && ![cell.progressLabel.text isEqualToString:@“Creating”]){ [cell setSelected:YES]; } NSArray * arrayOfThingsIWantToPassAlong = if(!isMaking){ isMaking = YES; } else { cell.progressLabel.hidden = NO; NSArray * arrayOfThingsIWantToPassAlong = if(!isMaking){
isMaking = YES; } |
和 getProgress 方法:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
–(void)getProgress:(NSArray*)args{
if (progManager == nil && !progStop){ progManager.responseSerializer = [AFJSONResponseSerializer serializer]; oneHundredPercent = @“100”; } } else{ [self getProgress:args]; } } |
任何建议都会对我有所帮助。我为这个问题头疼了两周。
- 您需要重构此代码并接受面向对象。使单元负责更新自身。 performSelector 几乎总是一种代码气味。您还将遇到问题,因为您正在从后台处理程序更新 UI,以及对 NSMutableArray 的非线程安全更新的潜在问题
- 几天前我遇到了同样的问题。检查 Rob 评论和 Aarons 示例。它可以帮助你:链接
我看到了您的代码,但使用这些大型方法有点难以理解。我不会跟踪数组中的处理单元。每个单元格都有一个对象来表示,这些对象有一个准备好的布尔值和一个进度值,对吧?所以尝试这样的事情:
- 确保每个单元格都有一个 progressView 作为子视图。
- 您的单元类应该有一个名为 styleForReady:(bool)isReady andProgress:(nsinteger)progress 的公共方法
- 为每个模型拨打服务电话以查看它们是否已完成。每当该服务调用返回时,您只需更新数组中的模型对象,并在它们具有新的进度值后执行 [self.tableView reloadData]。这将触发 numberOfRows(它应该返回 arrayOfObjects.count)和 cellForRowAtIndexPath:(它应该为那个 indexPath 出列单元格,获取代表那个单元格的模型,比如 arrayOfObjects[indexPath.row],最后,调用单元格来设置自己的样式基于该模型做 [cell styleForReady:objectModel.ready andProgress:objectModel.progress])
请记住,控制器应该只跟踪模型对象,将单元格出列并告诉单元格设置样式传递模型。不要将所有逻辑都放在控制器中。
- 谢谢你的回复!一时不明白:我现在应该在哪里获得每个对象的进度?在 objectModel 类或单元类中?你的意思是我必须在 UITableViewCell 的子类中实现我的 getProgress 方法吗?
- 控制器负责模型。每个模型(NSObject 的子类,应该有进度并准备好作为属性)。该单元负责绘制,您的单元中应该有一个 styleWithProgress: 和Ready: 方法,该方法将负责根据模型中的新进度和就绪值更新progressView。当进度调用返回时,您更新模型(不是单元格),然后调用 reloadData,这将触发根据新更新的模型绘制 tableViewCells 所需的所有 UI 方法。
- 好的,我在我的模型类(NSObject 的子类)中实现了方法 getProgress,并且在每次调用此方法时,我都会更改此类”readyProgress”属性的值,还实现了方法 -(void)styleForReady:(BOOL)isReady andProgress:(float)progress(我需要它的浮点值..没关系。现在在我的 cellForRowAtIndexPath 中,我检查未准备好的对象,然后调用 [cell styleForReady:NO andProgress:[oneItem getProgress]]; 但我不明白我应该在哪里调用 [self.tableView reloadData]; 这就是为什么我只能在滚动时看到更改
- 您不应该将状态放在单元格中。状态应保存在数据源中。
- @vendettacore 每次您想要更新任何单元格时,即每当您的模型有新值时,您都必须执行 [self.tableView reloadData] 。我会把它放在 viewWillAppear 中,每当你从服务器收到新数据时。 reloadData 调用 cellforRow 和 numberOfRows 方法,因此它将刷新所有可见单元格以及滚动时出现的单元格。
来源:https://www.codenong.com/29297148/