Edit mode with Custom Cells

2019-09-09 10:27发布

I've build a custom cell with IB, and display it on a tableView, that doesn't cover the whole window. I set up a toolbar and gave it a button, which toggles the isEditing attribute and the buttons title. I also made the if(!self.editing) in the didSelectRowAtIndexPath.

I get the feedback, that when the button is hit, I am in editing mode, but my custom cells don't show the delete-sign on the left. If I swipe a cell, the Delete button on the right appears, but the App crashes, if I push that button, but I'll address that later on, just thought I'd say this, in case that leads you to the mistake I made..

I've read, that it may happens that it doesn't display the lefthanded delete sign, if I don't assign my custom cell to the cell.contentview in cellforRowAtIndexPath. I tried, and got an error.

This is my first time with custom cells made in IB, so please bare with me, if I did a stupid mistake.

The code in cellForRowAtIndexPath, where I assign the custom cell:

static NSString *CellIdentifier = @"CustomCell";    
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {        
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];        
    for (id currentObject in topLevelObjects){
        if ([currentObject isKindOfClass:[UITableViewCell class]]){
            cell =  (CustomCell *) currentObject;
            break;
        }
    }        
}
// some more setup and stuff
return cell;

Thanks for any help!

2条回答
相关推荐>>
2楼-- · 2019-09-09 10:49

To make a custom cell create a subclass of UITableViewCell. Make a nib with a view and connect an outlet to it:

@interface CustomCell : UITableViewCell
{
    IBOutlet UIView* _cellView;
}

Override initWithStyle: reuseIdentifier: method and load the nib there. Add the custom view to the cell's content view:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        [_cellView setFrame:[[self contentView] bounds]];
        [[self contentView] addSubview:_cellView];
    }
    return self;
}

Then in the cellForRowAtIndexPath code you simply call:

if (cell == nil)
{
    cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier ] autorelease];
}
查看更多
够拽才男人
3楼-- · 2019-09-09 11:02

As my ViewController is a subclass of UIViewController, not UITableViewController, I would have needed to make the Edit button toggle like this:

- (IBAction)editButtonPressed:(id)sender {
   if (self.myTableView.isEditing) {
    self.editButton.title = @"Edit";
    self.myTableView.editing = NO;
   }
    else{
        self.editButton.title = @"Done";
        self.myTableView.editing = YES;
    }
}

Instead of

- (IBAction)editButtonPressed:(id)sender {
    if (self.myTableView.isEditing) {
        self.editButton.title = @"Edit";
        self.myTableView.editing = NO;     
    }
    else{
        self.editButton.title = @"Done";
        self.myTableView.editing= YES;
    }
}

Hope that helps someone else too..

查看更多
登录 后发表回答