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

发布于 2024-12-12 05:13:12 字数 1068 浏览 1 评论 0原文

我使用 IB 构建了一个自定义单元格,并将其显示在 tableView 上,该单元格不覆盖整个窗口。 我设置了一个工具栏并给了它一个按钮,用于切换 isEditing 属性和按钮标题。我还在 didSelectRowAtIndexPath 中创建了 if(!self.editing)

我收到反馈,当按下按钮时,我处于编辑模式,但我的自定义单元格左侧不显示删除符号。如果我滑动一个单元格,就会出现右侧的“删除”按钮,但是如果我按下该按钮,应用程序就会崩溃,但我稍后会解决这个问题,只是想我会这样说,在这种情况会导致您犯下我所犯的错误。

我读过,如果我不将自定义单元格分配给 cell.contentview中cellforRowAtIndexPath。我尝试过,但出现错误。

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;

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.

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

四叶草在未来唯美盛开 2024-12-19 05:13:12

要创建自定义单元格,请创建 UITableViewCell 的子类。创建一个带有视图的笔尖并将插座连接到它:

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

重写 initWithStyle:reuseIdentifier: 方法并在那里加载笔尖。将自定义视图添加到单元格的内容视图中:

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

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];
}
肤浅与狂妄 2024-12-19 05:13:12

由于我的 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;
    }
}

希望也能帮助其他人。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文