Objective c 表视图无/未更新
我有一个只有一个单元格的表视图。当我调用一个方法时,我尝试刷新它,文本标签的文本应该会改变。但是,它不会更改,仅当视图重新显示时才会更改 我在许多地方添加了Objective c 表视图无/未更新,objective-c,ios,xcode,uitableview,Objective C,Ios,Xcode,Uitableview,我有一个只有一个单元格的表视图。当我调用一个方法时,我尝试刷新它,文本标签的文本应该会改变。但是,它不会更改,仅当视图重新显示时才会更改 我在许多地方添加了NSLog(),并调用了正确的方法和条件 我试过几件事: reloadData setNeedsDisplay setNeedsLayout reloadCell reloadRowsAtIndexPaths: withRowAnimation: 但是什么都没用,我知道我的UITableViewDataSource代码没有问题,因为它被正确
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,在我尝试更新单元格时,我发现表视图为零,但我修改了单元格,不知道为什么
我没有在任何地方禁用或释放我的表视图,它仍然应该被分配
顺便说一下,我有一个选项卡栏控制器
更新:
我现在已经开始悬赏了,一个多星期之后,我真的觉得这个问题需要更多的关注和回答,我已经检查了我的代码多次,我无法看到为什么表视图没有更新/isnil
更新:
好的,我忘了提到用户可以滑动删除单元格。当他这样做时,它实际上并没有删除单元格,它只是更改了它的文本(即它没有更新的部分)。我将日志放在viewDidLoad
和viewdidappease
中,一旦用户滑动删除,表视图就会显示为有效。。。这是我的密码:
- (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];
}
总之,在加载/显示视图时,表视图是有效的。当用户滑动以删除时,它实际上不会删除该行,它只是调用另一个名为
TravelLogViewController
的视图控制器,方法名为manuallyDeleteTopCellData
,其中删除了一些数据,然后,该方法在我的原始视图控制器中调用另一个名为proceedWithDeletion
的方法,该方法应该重新加载表视图,但没有(问题)。这是因为在proceedWithDeletion
中,表视图为nil,但为什么呢 我通常使用以下代码:
[_myTable beginUpdates];
[... update my cell, adding / removing / changing them ...];
[_myTable endUpdates];
从5.0版开始,它就适用于我的项目。这是一个没有任何代码的盲镜头,但是,如果使用
dequeueReusableCellWithIdentifier
,可能会导致问题。如果您有一个单元格,则每次都可以在cellforrowatinexpath
中创建它,而不是重复使用已创建的单元格。使用如此小的表不会影响性能根据您的评论,您调用的reloadData
变量nil
常见的原因是在使用nib时缺少指向IBOutlet的链接、使用ARC的弱ivar或使用同名局部变量屏蔽ivar。我在代码中找不到任何错误。但是,请确保将表视图数据源和委托设置为(应在viewDidLoad/VIEWDIDISPENCE/VIEWWILLISPENCE中的某个位置设置): 在此之后,请确保从委托方法返回需要显示的行数:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [yourDataSource count];
}
祝你好运 在执行
TravelLogViewController
中的手动删除TopCell
时,此时将实例化一个新的菜单索引控制器
:
MenuMileIndexViewController *menu = [[MenuMileIndexViewController alloc] init];
[menu proceedWithDeletion];
这是一个不同的控制器,既没有正确初始化,也不负责屏幕上的视图。特别是,它没有相同的mostRecentFlight
属性。为了让TravelLogViewController
调用您的原始控制器,您需要将其作为参数传递给manuallyDeleteTopCell
,然后调用该控制器,例如如下所示
- (void)manuallyDeleteTopCell:(MenuMileIndexViewController *)origin {
[TheMileIndexViewController deductDesiredMilesToIndex: [[milesGainedIndex lastObject] floatValue]];
[locationArray removeLastObject];
[milesGainedIndex removeLastObject];
[dateArray removeLastObject];
[origin proceedWithDeletion];
}
并重写调用代码,如下所示:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
TravelLogViewController *travel = [[TravelLogViewController alloc] init];
[travel manuallyDeleteTopCellData:self];
}
以前的建议:
UIViewController
的子类)上设置插座,该控制器当前可见并包含在您正在使用的uitabarcontroller
实例的viewControllers
属性中不要将UITabBarController
子类化(请参阅明确禁止它的)[选中]viewDidLoad
方法和NSLog
self.tableView属性,以验证调用此方法时它是否为非nil
[选中]@synthesis mostRecentFlight=\u mostRecentFlight在TravelLogViewController
的@实现
中的code>语句,并在@界面
中的属性上设置断点。或者,手动为mostRecentFlight
属性实现setMostRecentFlight:
方法,并在此方法内设置断点。如果调用setter,将触发此断点,这将帮助您找到属性设置为nil
的时间
viewDidLoad
方法中的属性为非nil,请查看对象的地址(描述的开头写着(UITableView*)$1=0xXXXXXXXX
,其中0xXXXXXXXX
是地址)。在以后代码中要使用对象的位置设置断点(当属性为nil时)。在调试器po 0xXXXXXXXX
处键入(使用上面的适当地址)。你得到了什么样的回应我几乎可以肯定,问题不在你正在寻找或描述的领域 我从头开始创建了一个示例项目,非常简单地只需放置一个
UITableView
和一个UIButt
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
TravelLogViewController *travel = [[TravelLogViewController alloc] init];
[travel manuallyDeleteTopCellData:self];
}
BOOL toggle = NO;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: @"cell"];
if (cell) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (toggle )
{
NSDictionary *flight = @{@"name" : @"Bob",
@"date" : @"Yesterday",
@"miles" : @"3" };
cell.textLabel.text = [flight objectForKey: @"name"];
cell.textLabel.numberOfLines = 2;
cell.detailTextLabel.numberOfLines = 2;
cell.detailTextLabel.textColor = [UIColor blackColor];
cell.detailTextLabel.text = [NSString stringWithFormat:@"Flight Date: %@, Miles Gained: %.1f", [flight objectForKey: @"date"], [[flight objectForKey: @"miles"] doubleValue]];
} else {
cell.textLabel.text = @"blank label";
cell.detailTextLabel.text = @"blank detail";
}
}
return cell;
}
- (IBAction)toggleValue:(id)sender
{
toggle = !toggle;
NSLog( @"toggle is now %@", (toggle) ? @"YES" : @"NO" );
[tv reloadData];
}
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"PlayerCell"];
Player *player = [self.players objectAtIndex:indexPath.row];
cell.textLabel.text = player.name;
cell.detailTextLabel.text = player.game;
return cell;
MenuMileIndexViewController *menu = [[MenuMileIndexViewController alloc] init];
[menu proceedWithDeletion];
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
TravelLogViewController *travel = [[TravelLogViewController alloc] init];
[travel manuallyDeleteTopCellData:self];
}
}
- (void)manuallyDeleteTopCell:(id)sender {
[TheMileIndexViewController deductDesiredMilesToIndex: [[milesGainedIndex lastObject] floatValue]];
[locationArray removeLastObject];
[milesGainedIndex removeLastObject];
[dateArray removeLastObject];
// MenuMileIndexViewController *menu = [[MenuMileIndexViewController alloc] init];
[sender proceedWithDeletion];
}