Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setediting = true on tableview makes cell accessoryview disappear?

I have a UISwitchView on every tableview cell, and would like to allow the cells to be reordered. The problem is when I set setEditing=true on the table view, the accessory (switchview) disappears. Is there a way to keep both?

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] ;
like image 869
user1337645 Avatar asked Nov 23 '25 13:11

user1337645


2 Answers

UITableViewCell has an additional property, editingAccessoryView, for a view that is displayed when the cell is in editing mode.

I don't know if you can assign the same UISwitch instance to both the accessoryView and editingAccessoryView properties, or if you will need to make two instances. (A view can only have one parent at a time, but the UITableViewCell is actually doing the work of displaying the view, so it may be smart enough to handle this case.)

like image 147
benzado Avatar answered Nov 25 '25 05:11

benzado


yes, there is way to keep them.

you should make a custom cell view, you can add every UI elements to this custom cell, including the switch view (or anything else) and you won't be problem when you set the editing true of the table, because on the custom cell view the UI elements won't disappear.

the standard table cells's UI elements are being controlled by not you this is why you can see only one of the built-in accessories at same time. it will never happen when you use custom cell views.

I hope you are familiar with the custom cell views but if you are not, I can provide exact code as well but the idea would be this.

like image 23
holex Avatar answered Nov 25 '25 06:11

holex