表格视图在滚动时,UITableViewCell重新排序的控件消失了。

5

我的UITableView有两个自定义单元格:cell1和cell2。

我的UITableView根据枚举值显示其中之一。

如果enumValue == 0,则显示cell1,表格不应允许重新排序。 如果enumValue > 0,则显示cell2,表格应允许重新排序。

所有这些都运行良好。随着枚举值的更改,重新排序控件会适当地显示。

我的问题:当重新排序控件出现在单元格上时,当行滚动出视图时,它们将消失。为什么会发生这种情况?

对于格式和粗糙的代码,我很抱歉。我遇到了网站问题。是否有任何明显的问题可能会导致这种情况?

> // enum
> 
> -(void)setScorePhase:(phase)scorePhase {
> 
>     _scorePhase = scorePhase;
>     
>     if (scorePhase > score) {
>         self.btnPrevious.hidden = NO;
>         [self.tableCorps setEditing:YES animated:YES];
>     } else {
>         self.btnPrevious.hidden = YES;
>         [self.tableCorps setEditing:NO animated:YES];
>     }
>     
>     switch (scorePhase) {
>         case score:
>             self.lblInstructions.text = @"Give your scores";
>             self.btnNext.titleLabel.text = @"Next";
>             break;
>         case bestdrums:
>             self.lblInstructions.text = @"Order the corps by best percussion";
>             self.btnNext.titleLabel.text = @"Next";
>             break;
>         case besthornline:
>             self.lblInstructions.text = @"Order the corps by best hornline";
>             self.btnNext.titleLabel.text = @"Next";
>             break;
>         case bestguard:
>             self.lblInstructions.text = @"Order the corps by best colorguard";
>             self.btnNext.titleLabel.text = @"Next";
>             break;
>         case loudesthornline:
>             self.lblInstructions.text = @"Order the corps by loudest hornline";
>             self.btnNext.titleLabel.text = @"Next";
>             break;
>         case favorite:
>             self.lblInstructions.text = @"Order the corps by your favorite";
>             self.btnNext.titleLabel.text = @"Submit";
>             break;
>             
>         default:
>             self.lblInstructions.text = @"Error";
>             break;
>     }
>     [self.tableCorps reloadData]; 
}




>

        -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    >         
    >         return 2;
    >     }
    >     
    >     -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    >         
    >         switch (section) {
    >             case 0: return @"World Class";
    >             case 1: return @"Open Class";
    >             default: return @"Error";
    >         }
    >     }
    >     
    >     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    >         
    >         if ([self.arrayOfPerformingCorps count]) {
    >             switch (section) {
    >                 case 0: return [self.arrayOfWorldClass count];
    >                 case 1: return [self.arrayOfOpenClass count];
    >                 default: return 0;
    >             }
    >         } else {
    >             return 0;
    >         }
    >     }
    >     
    >     -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    >         
    >         UITableViewCell *cell;
    >         if (self.scorePhase == score) {
    >             cell = [self.tableCorps dequeueReusableCellWithIdentifier:@"cell1"];
>cell.showsReorderControl = NO;
    >             
    >         } else {
    >             cell = [self.tableCorps dequeueReusableCellWithIdentifier:@"cell2"];
>cell.showsReorderControl = YES;
    >             
    >         }
    >         
    >         
    >         PFObject *corps;
    >         if ([self.arrayOfPerformingCorps count]) {
    >             if ((int)[indexPath section] == 0) {
    >                 corps = [self.arrayOfWorldClass objectAtIndex:[indexPath row]];
    >             } else {
    >                 corps = [self.arrayOfOpenClass objectAtIndex:[indexPath row]];
    >             }
    >             
    >             UILabel *corpsNameLabel = (UILabel *)[cell viewWithTag:0];
    >             corpsNameLabel.text = corps[@"corpsName"];
    >             
    >             
    >         } else {
    >             //cell.textLabel.text = @"";
    >             //cell.detailTextLabel.text = @"";
    >         }
    >         
    >         return cell;
    >     }
    >     
    >     #pragma  mark - Table Reordering
    >     - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    >         if (self.scorePhase > score) return YES;
    >         else return NO;
    >     }
    >     - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    >         return UITableViewCellEditingStyleNone;
    >     }
    >     - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    >         return NO;
    >     }
    >     - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    >         return YES;
    >     }
    >     - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
    > toIndexPath:(NSIndexPath *)destinationIndexPath{
    >         
    >     }
    >     
    >     - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath
    > *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
    >     {
    >         if (sourceIndexPath.section != proposedDestinationIndexPath.section) {
    >             NSInteger row = 0;
    >             if (sourceIndexPath.section < proposedDestinationIndexPath.section) {
    >                 row = [tableView numberOfRowsInSection:sourceIndexPath.section] - 1;
    >             }
    >             return [NSIndexPath indexPathForRow:row inSection:sourceIndexPath.section];
    >         }
    >         
    >         return proposedDestinationIndexPath;
    >     }

经过进一步的测试,我相信这是一个iOS 8测试版的问题。 - Justin Moore
我也遇到了同样的问题。我倾向于认为这也是iOS 8测试版的问题。 - cybervedaa
5个回答

18

这是iOS 8的一个bug。一个简单的解决方法是在UITableViewCell子类中覆盖prepareForReuse,像这样:

- (void)prepareForReuse
{
    [super prepareForReuse];
    [self setEditing:NO animated:NO];
}

1
非常感谢!顺便提一下,这在iOS 8.1上也发生了,似乎不是Beta版本的问题。 - kacho
1
我也在8.1中看到了这个问题,如果您没有使用UITableViewCell子类,只需在tableView:cellForRowAtIndexPath:调用中执行cell.editing = NO即可。 - Javawag

2

我在一个表格视图中遇到了相同的问题,其中有可重排列的单元格和不可重排列的单元格。我尝试了几种解决方案(使用不同的可重用标识符等),最终找到的解决方法是在我的 setter 方法中设置 showsReorderControl 属性为 YES。

self.showsReorderControl = reorderable;
[self setEditing:reorderable animated:NO];

希望这能帮到您。

1
+1. 如果您将其用于UITableViewCell子类中,或者在willDisplayCell UITableView委托方法中使用此代码来处理单元格,则似乎可以解决问题。 - Mustafa
@Mustafa,我希望我对你的评论的点赞能够给你一些声望。;) 在willDisplayCell方法中使用这个是一个很棒的建议 :) - 0xKayvan

0

我一直遇到同样的问题...在IOS 8 beta 2上。

我已经发现问题出现在单元格“dequeueReusableCellWithIdentifier”区域的某个地方。一旦单元格被滚动并使用可重用单元格,所有删除和移动图像都会消失。

作为一种解决方法(此代码仍在测试中),我删除了重用和注册单元格的代码...

tableViewFromNib.registerClass(myViewControllerCell.self, forCellReuseIdentifier: cellReuseID)
     .
     .
let cell = tableView!.dequeueReusableCellWithIdentifier(cellReuseID, forIndexPath: indexPath) as myViewControllerCell 

并将其替换为以下内容(在cellForRowAtIndexPath中).....

let cell = UITableViewCell()

......这个方法是能解决问题的。我知道这不是一个真正的解决办法(很多未使用的单元格飞来飞去),但它让我能继续编码。当问题被解决后,我会回来添加出列代码。

顺便说一句...这可能本应该是一个评论,但我刚刚开始使用stackoverflow,还没有足够的积分进行评论。


0

我遇到了与iOS 8 beta版本相同的问题。

我发现当表格处于编辑模式时,UITableViewCell中有两个视图负责表格编辑; 这些是UITableViewCellEditControl和UITableViewCellReorderControl。 这两个视图在第一次创建表格时都存在,一切正常,但随着表格重新加载/滚动,这些视图会消失。

我认为这个问题是Beta版本的问题,我尝试了iOS 8 Beta 2版本,问题仍然存在。

我们可能需要等待下一个版本的发布。


3
仍然存在于 iOS 8 发布版本中。 - Vadim Yelagin

-1

iOS 8测试版问题。不是代码问题。


好的,但这不是解决方案! - 0xKayvan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接