6

我有一个UITableView在编辑模式下使用以下行自动设置为多项选择viewDidLoad

self.tableView.allowsMultipleSelectionDuringEditing = YES;
[self setEditing:YES animated:YES];

但是,我想通过更改其背景颜色而不是通过自动出现在每行左侧的复选标记来表明选择了一行。(例如,在邮件应用程序中编辑电子邮件列表时出现的那些,或者在这个 SO 问题中讨论的那些。)我已经让它大部分工作,除了我无法获得那些复选框,它们是作为UITableView进入编辑模式的一部分自动创建,然后消失。

下面是我正在使用的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _Hierachy.cellCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *testCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(testCell == nil) {
        testCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [[testCell textLabel] setText:@"Test Cell"];

    return testCell;
}

这些是UITableView我到目前为止唯一的方法,所以其他一切都应该是默认行为。

有谁知道如何在编辑模式下隐藏左侧的那些复选标记?我在单元格的附件部分看到了很多关于复选标记的问题,但据我了解,这是另一回事。我还看到人们谈论该tableView:didSelectRowAtIndexPath:方法,但这些复选标记是在表格进入编辑模式时创建的,并在用户点击“完成”时关闭,因此该方法似乎不相关。

我最接近的是找到这种方法:

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}

但这只是防止单元格的内容缩进为复选标记腾出空间。复选标记仍然出现。

当然有一种方法可以隐藏这些复选标记,并且仍然允许在编辑模式下进行多项选择?还是UITableView在启用多选的编辑模式下,这些复选标记是严重的强制性行为?

编辑:我(不情愿地)对有点老套的答案持开放态度,比如移动复选标记的框架直到它离开屏幕。此应用仅供内部使用,无需获得 App Store 批准。但是鉴于在UITableView进入编辑模式时会自动创建复选标记,我什至不知道如何将它们作为要更改的对象。任何帮助,将不胜感激!

4

3 回答 3

10

你必须继承你的 UITableViewCell 并覆盖这样的(void)setEditing:animated:方法:

#import "MyCustomCell.h"

@implementation MyCustomCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setSelectedBackgroundView:(UIView *)selectedBackgroundView
{
    //Cell Selected Color: CLEAR
    [super setSelectedBackgroundView:selectedBackgroundView];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    //Cell Edit Mode NO Indent & Selected Color: CLEAR
    [super setEditing:NO animated:animated];
    [self setNeedsLayout];
}

@end

完成此操作后,转到Inteface Builder并让您的单元格成为班级的一部分MyCustomCell

在 IB 中使您的单元格成为 MyCustomCell 类的一部分后,在您的代码中导入MyCustomCell.hUITableViewController修改以下内容:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCustomCell *testCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(testCell == nil) {
        testCell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [[testCell textLabel] setText:@"Test Cell"];

    return testCell;
}

更新:您还可以在 TableView 中执行以下操作tableView:editingStyleForRowAtIndexPath:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{
            return UITableViewCellEditingStyleNone;
}

但是你会让你的单元格缩进。要删除该缩进,您必须对 Cell 进行子类化。

这样做后你应该很好!我刚刚对其进行了测试,它可以按照您想要的方式工作!

于 2014-10-03T18:28:52.763 回答
1

这是不带复选标记的多选最简单的解决方案:

- (BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath {
    return NO;
}

这将导致使用默认选择样式(灰色或使用您的自定义选择背景)选择单元格,并且不会出现复选标记。


关于您选择的任何解决方案的一句话。用户期望跨多个应用程序获得一致的体验,而这些复选标记是这种一致性的一部分。确保有充分的理由更改正常的操作系统外观。

于 2014-10-03T18:42:07.337 回答
0

这是如何实现的:

  1. 滑动删除作品
  2. 在编辑模式下,您在单元格左侧看不到复选框、删除项目或其他任何内容
  3. 单元格缩进仍然正常工作(如果需要,您可以将其关闭)
  4. (红利)支持多选

所以 - 在你的 UITableViewDelegate

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isEditing)
    {
      return UITableViewCellEditingStyleNone;
    }
    else
    {
      return UITableViewCellEditingStyleDelete;
    } 
}

- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{
  (do stuff)
}

你还需要配置你的 UITableView

self.table.allowsMultipleSelectionDuringEditing=NO;

现在,如果您确实想要多项选择;

self.table.allowsSelectionDuringEditing=YES;

然后自己管理选定的单元格。

我在 UITableViewCell 子类中放置了一个自定义复选框,并且我还更改了值以响应

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

提问者想用背景颜色表示选择 - 这部分应该是直截了当的。

于 2015-11-27T18:54:15.383 回答