【问题标题】:Multi Select Table View Cell and no Selection Style多选表格视图单元格和无选择样式
【发布时间】:2011-02-03 19:46:48
【问题描述】:

我有一个基本的 UITableView,我想在没有选择样式的情况下启用 Mail.app 样式复选标记。我有以下sn-p:

#define UITableViewCellEditingStyleMultiSelect (3)

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleMultiSelect;
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

但是,这将永远不会在选择时显示复选标记(尽管会显示空圆圈)。关于如何解决它的任何想法?我知道它使用了未记录的功能,但我真的很想添加对复选标记的支持。我的实际示例使用了一个非常自定义的UITableViewCell,我无法启用选择样式!

【问题讨论】:

    标签: iphone cocoa-touch uitableview


    【解决方案1】:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
        if (self.tableView.isEditing) {
            cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        } else {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    
        return cell;
    }
    
    -(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
        return UITableViewCellEditingStyleMultiSelect;
    }
    
    -(IBAction) switchEditing {
        [self.tableView setEditing:![self.tableView isEditing]];
        [self.tableView reloadData]; // force reload to reset selection style
    }
    

    【讨论】:

    • 感谢您的回复!这几乎完美无缺!您知道是否可以删除出现在选定单元格上的青绿色高光?
    • 这似乎是不可能的,因为在调用 willSelectRowAtIndexPath 之后插入了绿松石高亮视图 (UITableViewCellSelectedBackground)。如果我在 didSelectRowAtIndexPath 中删除它,它在单元格被触摸时仍然可见。
    • 也许你可以继承 UITableViewCell 并覆盖 setSelected:animated。
    • 谢谢。我可能最终会选择第二个选项。非常感谢您的回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多