开发者

Custom editingAccessoryView behaving unpredictable

开发者 https://www.devze.com 2023-03-17 23:42 出处:网络
Okay, first off - how I want my cells to look in my UItableView in editing mode (with some nicer buttons):

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

Custom editingAccessoryView behaving unpredictable

However - this is how it looks right now:

Custom editingAccessoryView behaving unpredictable

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.


I suggest you subclass table cell and override following method

- (void)layoutSubviews
{
    if([self isEditing])
    {    
        // your custom stuff
       [self.contentView addSubview:myButton];
    }
}


You need to make a new accessoryView for every cell.


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
     }   
 }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消