3

Okay, first off - how I want my cells to look in my UItableView in editing mode (with some nicer buttons):

enter image description here

However - this is how it looks right now:

enter image description here

My problem is that my custom EditingAccessoryView only appears on the cell that I first created, and that those circle-thingies (what are those called?) appears. Which doesn't do much good.

Now, my code looks like this (which seems like the common way of doing this, seen at this question for example: How to add Custom EditingAccessoryView for UITableView?)

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.editingAccessoryView = AccessoryView;
        AccessoryView.backgroundColor = [UIColor clearColor];
    }

I have read that you are supposed to be able to call your custom editingAccessoryView by swiping, even if - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath returns "NO". This however I have not been able to achieve.

So, to sum it upp; I want my editingAccessoryView to be displayed at all cells - and, if possible, remove the red circle. Or, alternatively, call my editingAccessoryView when I swipe - what method gets called when the user does this?

Any help would be much appreciated.

Community
  • 1
  • 1
Marcus Olsson
  • 2,485
  • 19
  • 35

4 Answers4

1

I suggest you subclass table cell and override following method

- (void)layoutSubviews
{
    if([self isEditing])
    {    
        // your custom stuff
       [self.contentView addSubview:myButton];
    }
}
Robert Childan
  • 983
  • 1
  • 12
  • 22
0

To remove red circles you can return UITableViewCellEditingStyleNone in

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

But in this case the empty space for that circle is still there. I'm almost sure, it can be removed too.

My problem is that my custom EditingAccessoryView only appears on the cell that I first created

Try to instantiate new AccessoryView for each cell instead of using the same one.

zxcat
  • 2,054
  • 3
  • 26
  • 40
0

Instead of setting all this in the cellForRowAtIndexPath.... do this.

In cellForRowAtIndexPath

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

And set your accessory view in

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 {
     if (editingStyle == UITableViewCellEditingStyleDelete) {
         // Delete the row from the data source
         NSLog(@"Am I Editing");

         UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

         selectedCell.editingAccessoryView = AccessoryView;
         AccessoryView.backgroundColor = [UIColor clearColor]
     }   
     else if (editingStyle == UITableViewCellEditingStyleInsert) {
         // Create a new instance of the appropriate class,
         //   insert it into the array, and add a new row to the table view
     }   
 }
Kjuly
  • 34,476
  • 22
  • 104
  • 118
Legolas
  • 12,145
  • 12
  • 79
  • 132
  • Does this happen when you click the `edit` on top and then it displays the picture you have listed ? – Legolas Jul 05 '11 at 17:02
  • Yeah - when I touch the edit-button and the UITableView enters edit-mode, the red circles appear - and the bottom cell gets the custom editingAccessoryView (pic no. 2) – Marcus Olsson Jul 05 '11 at 17:04
  • Yet again - sorry pal =( Neither did it work in "- (void)tableView: (UITableView *)tableView willDisplayCell: (UITableViewCell *)cell forRowAtIndexPath: (NSIndexPath *)indexPath" nor "- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { " =/ – Marcus Olsson Jul 05 '11 at 17:24
  • How does no one seem to know how to do this? So many of these questions have answers that don't work. You'd think this would be easier. – Dustin Dec 09 '16 at 19:56
0

You need to make a new accessoryView for every cell.

Benjamin Mayo
  • 6,649
  • 2
  • 26
  • 25
  • In what way? I tried to configure the accessoryView at the same place in the code as I configured the labels text - but no luck there either. – Marcus Olsson Jul 06 '11 at 08:00
  • You only have a single instance of your editing accessory view. Each time you assign it to a new table cell, I suspect it's removed from the previous one - this would be why it only appears in the bottom row. You need to instantiate a new view for each cell. – Thom Sep 14 '12 at 13:50