首页 文章

tableview上的setediting = true会使cell accessoryview消失吗?

提问于
浏览
0

我在每个tableview单元格上都有一个UISwitchView,并且希望允许对单元格进行重新排序 . 问题是当我在表视图上设置setEditing = true时,附件(switchview)消失了 . 有没有办法保持两者?

EDIT

static NSString *CellIdentifier = @"Edit Sources Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

// Configure the cell...
NSUInteger row = [indexPath row];
Source *source = [self.sources objectAtIndex:row];

cell.textLabel.text = source.name;

UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
switchView.tag = row;
cell.accessoryView = switchView;
[switchView setOn:source.switchedOn animated:NO];
[switchView addTarget:self action:@selector(switchChangedForSource:)  forControlEvents: UIControlEventValueChanged] ;

4 回答

  • 4

    UITableViewCell有一个附加属性 editingAccessoryView ,用于在单元格处于编辑模式时显示的视图 .

    我不知道您是否可以为 accessoryVieweditingAccessoryView 属性分配相同的UISwitch实例,或者您是否需要创建两个实例 . (一个视图一次只能有一个父项,但UITableViewCell实际上是在显示视图的工作,所以它可能足够智能来处理这种情况 . )

  • 1

    如果您的意思是它正在消失 under 重新排序控件,您可以通过调整 autoresizingMask 属性将其重新定位到其他位置(如果您已在IB中构建它) .

  • 0

    是的,有办法保留它们 .

    当你设置表格的编辑为true时,你应该制作 a custom cell viewyou can add every UI elements to this custom cell ,包括切换视图(或其他任何东西)和 you won't be problem ,因为在自定义单元格视图中,UI元素不会消失 .

    标准表格单元格的UI元素由您控制,这就是为什么您只能同时看到其中一个内置附件的原因 . 当您使用自定义单元格视图时,它永远不会发生 .

    我希望您熟悉自定义单元格视图,但如果您不熟悉,我也可以提供准确的代码,但想法就是这样 .

  • 1

    我认为你需要子类化你的tableview单元格 . 如果 self.isEditingUITableViewCell 子类为 self.isEditing ,则以编程方式将 UISwitch 移动到左侧 .

相关问题