使用自定义单元格的编辑模式 [英] Edit mode with Custom Cells

查看:35
本文介绍了使用自定义单元格的编辑模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用 IB 构建了一个自定义单元格,并将其显示在一个 tableView 上,它不会覆盖整个窗口.我设置了一个 toolbar 并给了它一个按钮,它可以切换 isEditing 属性和按钮标题.我还在 didSelectRowAtIndexPath 中制作了 if(!self.editing).

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

我读过,如果我不将我的自定义单元格分配给 cellforRowAtIndexPath<中的 cell.contentview ,它可能会发生它不显示左手删除符号的情况/代码>.我试过了,但出现错误.

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.

cellForRowAtIndexPath 中的代码,我在其中分配了自定义单元格:

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;

推荐答案

由于我的 ViewController 是 UIViewController 的子类,而不是 UITableViewController,所以我需要像这样切换编辑按钮:

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

代替

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

希望也能帮到别人..

这篇关于使用自定义单元格的编辑模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆