Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 使用自定义单元格的编辑模式_Objective C_Uitableview_Editing - Fatal编程技术网

Objective c 使用自定义单元格的编辑模式

Objective c 使用自定义单元格的编辑模式,objective-c,uitableview,editing,Objective C,Uitableview,Editing,我用IB构建了一个自定义单元格,并将其显示在一个不覆盖整个窗口的tableView上。 我设置了一个工具栏并给它一个按钮,它可以切换iEdit属性和按钮标题。我也做了假设!在didSelectRowAtIndexPath中进行自我编辑 我得到的反馈是,当按下按钮时,我处于编辑模式,但我的自定义单元格没有在左侧显示删除标志。如果我刷了一个单元格,右边的删除按钮就会出现,但如果我按下那个按钮,应用程序就会崩溃,但我会在以后解决这个问题,我想我会这么说,以防这会导致你犯我犯的错误 我已经读到,如果我不

我用IB构建了一个自定义单元格,并将其显示在一个不覆盖整个窗口的tableView上。 我设置了一个工具栏并给它一个按钮,它可以切换iEdit属性和按钮标题。我也做了假设!在didSelectRowAtIndexPath中进行自我编辑

我得到的反馈是,当按下按钮时,我处于编辑模式,但我的自定义单元格没有在左侧显示删除标志。如果我刷了一个单元格,右边的删除按钮就会出现,但如果我按下那个按钮,应用程序就会崩溃,但我会在以后解决这个问题,我想我会这么说,以防这会导致你犯我犯的错误

我已经读到,如果我不将自定义单元格分配给cellforRowAtIndexPath中的cell.contentview,它可能不会显示左手删除符号。我试过了,但还是出错了

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

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;

要创建自定义单元格,请创建UITableViewCell的子类。制作具有视图的笔尖,并将插座连接到该笔尖:

@interface CustomCell : UITableViewCell
{
    IBOutlet UIView* _cellView;
}
重写initWithStyle:reuseIdentifier:method并在那里加载nib。将自定义视图添加到单元格的内容视图:

- (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;
}
然后在cellForRowAtIndexPath代码中,您只需调用:

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

由于我的ViewController是UIViewController的子类,而不是UITableViewController,因此我需要使编辑按钮切换如下:

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

希望这对其他人也有帮助。

Okkkaaaay,对不起,那真是太愚蠢了,尽管它可能发生。。由于我的ViewController是UIViewController的一个子类,而不是UITableViewController,我需要让编辑按钮切换self.MyTableView.editing..oO我不太清楚,这样做的好处是什么,它破坏了我的整个设置--设置的目的是什么?