【问题标题】:Custom editingAccessoryView behaving unpredictable自定义编辑AccessoryView 行为不可预测
【发布时间】:2011-07-05 16:11:25
【问题描述】:

好的,首先 - 我希望我的单元格在编辑模式下在 UItableView 中的外观(带有一些更好的按钮):

但是 - 这就是现在的样子:

我的问题是我的自定义 EditingAccessoryView 只出现在我第一次创建的单元格上,并且那些圆形的东西(那些叫什么?)出现了。这没有多大用处。

现在,我的代码看起来像这样(这似乎是这样做的常见方式,例如在这个问题上看到:How to add Custom EditingAccessoryView for UITableView?

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.editingAccessoryView = AccessoryView;
        AccessoryView.backgroundColor = [UIColor clearColor];
    }

我读到你应该能够通过滑动调用你的自定义editingAccessoryView,即使- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 返回“NO”。然而,这我一直无法实现。

所以,总结一下;我希望在所有单元格中显示我的 editingAccessoryView - 如果可能,请删除红色圆圈。或者,或者,在我滑动时调用我的 editingAccessoryView - 当用户这样做时会调用什么方法?

任何帮助将不胜感激。

【问题讨论】:

    标签: iphone objective-c ios ios4


    【解决方案1】:

    我建议你继承表格单元格并覆盖以下方法

    - (void)layoutSubviews
    {
        if([self isEditing])
        {    
            // your custom stuff
           [self.contentView addSubview:myButton];
        }
    }
    

    【讨论】:

    • 这是正确的答案。非常感谢@richardchildan
    【解决方案2】:

    您需要为每个单元格创建一个新的附件视图。

    【讨论】:

    • 以什么方式?我尝试在代码中配置标签文本的同一位置配置附件视图 - 但那里也没有运气。
    • 您只有一个编辑附件视图的实例。每次你将它分配给一个新的表格单元格时,我怀疑它已从前一个单元格中删除 - 这就是它只出现在底行的原因。您需要为每个单元格实例化一个新视图。
    【解决方案3】:

    不要在cellForRowAtIndexPath....中设置所有这些。

    cellForRowAtIndexPath

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    

    并在

    中设置您的附件视图
     - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
     {
         if (editingStyle == UITableViewCellEditingStyleDelete) {
             // Delete the row from the data source
             NSLog(@"Am I Editing");
    
             UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    
             selectedCell.editingAccessoryView = AccessoryView;
             AccessoryView.backgroundColor = [UIColor clearColor]
         }   
         else if (editingStyle == UITableViewCellEditingStyleInsert) {
             // Create a new instance of the appropriate class,
             //   insert it into the array, and add a new row to the table view
         }   
     }
    

    【讨论】:

    • 当你点击顶部的edit然后它显示你列出的图片时会发生这种情况吗?
    • 是的 - 当我触摸编辑按钮并且 UITableView 进入编辑模式时,出现红色圆圈 - 底部单元格获取自定义的 editingAccessoryView(图片编号 2)
    • 再一次 - 抱歉,朋友 =( 它在“- (void)tableView: (UITableView *)tableView willDisplayCell: (UITableViewCell *)cell forRowAtIndexPath: (NSIndexPath *)indexPath" 或“- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { " =/
    • 似乎没有人知道如何做到这一点?这些问题中有很多的答案是行不通的。你会认为这会更容易。
    【解决方案4】:

    要删除红色圆圈,您可以在

    中返回UITableViewCellEditingStyleNone
    - (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath;
    

    但在这种情况下,那个圆圈的空白空间仍然存在。我几乎可以肯定,它也可以删除。

    我的问题是我的自定义 EditingAccessoryView 只出现在我第一次创建的单元格上

    尝试为每个单元格实例化新的AccessoryView,而不是使用同一个。

    【讨论】:

      猜你喜欢
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-28
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多