【问题标题】:UITableView slide not editable row with editable rows Xcode iPhoneUITableView幻灯片不可编辑行与可编辑行Xcode iPhone
【发布时间】:2012-01-31 16:54:53
【问题描述】:

我正在开发一款 iPhone 应用程序,但遇到了一个问题。我有一个 UITableView 有几个可编辑的行(

 -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ )

还有一个不可编辑的行。如果我点击编辑,可编辑的行会​​向右滑动一点,左侧有一个红色的圆形按钮,但不可编辑的行根本不会滑动。有没有办法把它也滑到右边但左边没有红色按钮?目前看起来不太好。我希望有人可以帮助我:)

【问题讨论】:

  • 您是使用自定义单元格,还是只是自定义一个内置单元格?

标签: iphone objective-c ios xcode uitableview


【解决方案1】:

我不确定更改 tableView 的默认行为是否是个好主意。 但如果你真的想,你可以例如使用缩进。

// Might be target of button
- (void) setEditingMode
{
    tableView.editing = YES;
    [tableView reloadData];
}

// Might be target of button
- (void) resetEditingMode
{
    tableView.editing = NO;
    [tableView reloadData];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = ...;
    ....
    Boolean cellIsEditable = ...;
    if( tableView.editing && !cellIsEditable )
    {
        cell.indentationWidth = ...; // (please experiment to find the exact value)
        cell.indentationLevel = 1;
    }
    else
    {
        cell.indentationLevel = 0;
    } 
}

【讨论】:

  • 哦抱歉我忘了说它是一个分组的tableView。但这只会将文本向右滑动,但单元格仍然是全尺寸:S
  • 我自己解决了这个问题:) 但仍然感谢您的帮助!:)
  • @kjeldGr - 解决方案是什么?如果您愿意,您可以回答自己的问题(并使用计票下方的勾号将其标记为答案),这样可以帮助其他偶然发现这个问题的人
【解决方案2】:

继承 UITableViewCell 并自己滑动不可编辑的行。

@interface MyHistoryTableViewCell : UITableViewCell
@end

@implementation MyHistoryTableViewCell : UITableViewCell

#define CELL_SLIDE_WIDTH    32  // found empirically
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    if ( self.editingStyle == UITableViewCellEditingStyleNone ) { // not editable
        CGRect frame = self.frame;
        UITableView *tableView = ((UITableView *)(self.superview));
        if ( tableView.editing ) { // going to editing mode
            frame.origin.x = CELL_SLIDE_WIDTH;
            frame.size.width = tableView.frame.size.width - CELL_SLIDE_WIDTH;
        } else {  // ending editing
            frame.origin.x = 0;
            frame.size.width = tableView.frame.size.width;
        }
        [UIView animateWithDuration:0.3 animations:^{ // match the tableView slide duration
            self.frame = frame;
        }];
    }
}
@end

如果您有需要锚定在单元格右侧的子视图(例如,不应滑动的按钮),那么就这样做

mySubview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; // anchors to the right margin

另外,在设置子视图的 frame.origin.x 时,你必须要有创意。我尝试了许多值,直到我找到了可行的方法(该值对我来说毫无意义)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    相关资源
    最近更新 更多