Custom editingAccessoryView behaving unpredictable

*爱你&永不变心* 提交于 2020-02-02 06:40:05

问题


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

However - this is how it looks right now:

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.


回答1:


I suggest you subclass table cell and override following method

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



回答2:


You need to make a new accessoryView for every cell.




回答3:


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



回答4:


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.



来源:https://stackoverflow.com/questions/6585681/custom-editingaccessoryview-behaving-unpredictable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!