objective-c - TableView 无/不更新

标签 objective-c ios xcode uitableview

我有一个只有一个单元格的表格 View 。当我调用一个方法时,我尝试刷新它并且文本标签的文本应该改变。但是它不会,它只会在 View 重新出现时发生变化。

我在很多地方都添加了NSLog()并且调用了正确的方法和条件

我已经尝试了一些事情:

reloadData
setNeedsDisplay
setNeedsLayout
reloadCell
reloadRowsAtIndexPaths: withRowAnimation:

但是没有起作用,我知道这不是我的UITableViewDataSource 代码的问题,因为它被正确地调用了。

这是我能给你的最合适的代码:

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

    tableView.scrollEnabled = NO;

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: @"cell"];

    if (cell) {

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        if ([[TravelLogViewController locationArray] count]) {

            NSDictionary *flight = [NSDictionary dictionaryWithObjectsAndKeys:[[TravelLogViewController locationArray] lastObject], @"name", [[TravelLogViewController dateArray] lastObject], @"date", [[TravelLogViewController milesGainedIndex] lastObject], @"miles", nil];

            cell.detailTextLabel.textColor = [UIColor blackColor];

            cell.textLabel.numberOfLines = 2;
            cell.detailTextLabel.numberOfLines = 2;

            cell.textLabel.font = [UIFont fontWithName:@"Avenir" size: 14.0];
            cell.detailTextLabel.font = [UIFont fontWithName:@"Avenir" size: 12.0];

            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            cell.textLabel.text = [flight objectForKey: @"name"];

            cell.detailTextLabel.text = [NSString stringWithFormat:@"Flight Date: %@, Miles Gained: %.1f", [flight objectForKey: @"date"], [[flight objectForKey: @"miles"] doubleValue]];

            cell.backgroundColor = [UIColor colorWithWhite:0.92 alpha:1];
            //        cell.backgroundColor = [UIColor clearColor];
        }
        else {

// This gets called when I want it to, but the textLabel text or detailTextLabel text is not changed


            cell.detailTextLabel.text = nil;
            cell.textLabel.text = nil;
            cell.textLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size: 18.0];
            cell.textLabel.text = @"\n\n                 No Flights";
        }
    }

    return cell;
}

更新:

我正在使用 ARC,我发现在我尝试更新单元格时表格 View 为零,但我修改了我的单元格并且不知道为什么!

我没有在任何地方取消或释放我的 TableView ,它仍然应该被分配

顺便说一句,我有一个标签栏 Controller

更新:

我现在已经开始悬赏了,一个多星期后,我真的觉得这个问题需要更多的关注和回答,我已经多次检查我的代码,但我无法明白为什么表格 View 没有更新/是 nil

更新:

好吧,我忘了说用户可以通过滑动来删除单元格。当他这样做时,它实际上并没有删除 单元格,它只是更改了它的文本(即它没有更新的部分)。我将日志放入 viewDidLoadviewDidAppear 并且 TableView 似乎有效,一旦用户滑动删除...这是我的代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {

    TravelLogViewController *travel = [[TravelLogViewController alloc] init];
    [travel manuallyDeleteTopCellData]; // Deletes some data in other view controller and calls the method `proceedWithDeletion`
}
}
- (void)proceedWithDeletion {

// Method is called from another view controller

NSLog(@"%@", mostRecentFlight.description); // NIL HERE

[mostRecentFlight reloadData];
[mostRecentFlight setNeedsDisplay];
[mostRecentFlight setNeedsLayout];

}

TravelLogViewController

- (void)manuallyDeleteTopCell {

[TheMileIndexViewController deductDesiredMilesToIndex: [[milesGainedIndex lastObject] floatValue]];

[locationArray removeLastObject];

[milesGainedIndex removeLastObject];

[dateArray removeLastObject];

MenuMileIndexViewController *menu = [[MenuMileIndexViewController alloc] init];
[menu proceedWithDeletion];

}

所以总结一下,一个 View 加载/出现, TableView 是有效的。当用户滑动删除时,它不会实际上删除它只是调用另一个名为 TravelLogViewController 的 View Controller 方法调用 manuallyDeleteTopCellData 删除一些数据,该方法然后在我的原始 View Controller 中调用另一个方法,称为 proceedWithDeletion,它应该重新加载 TableView ,但没有(问题)。这样做的原因是因为在 proceedWithDeletion 中 TableView 为 nil,但为什么呢!?

最佳答案

这是一个没有任何代码的盲目拍摄,但是,如果您使用 dequeueReusableCellWithIdentifier,它可能会导致问题。如果您有一个单元格,则每次都可以在 cellForRowAtIndexPath 中创建它,而不是重复使用已经创建的单元格。不会影响这么小的表的性能

关于objective-c - TableView 无/不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12742815/

相关文章:

ios - 亚马逊 Cognito 与 SwiftUI

ios - Apple 如何计算 iAd 横幅的展示次数?

显示带有约束的 View Controller 时,xcode 6 界面构建器的性能非常低

ios - SwiftUI 中水平居中 View

objective-c - Objective C 数学公式失败

ios - 是否可以从 UIImage 中剥离或完全删除 UIImageOrientation 信息?

ios - 对于像 android CoordinatorLayout 这样的 iOS

objective-c - Cocoa 的周 View 控制(Apple Mac OS X 开发)

iphone - 将 subview 发送到后面

ios - 如何使用 NSRange