使用自定义单元格的编辑模式
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要创建自定义单元格,请创建 UITableViewCell 的子类。创建一个带有视图的笔尖并将插座连接到它:
重写 initWithStyle:reuseIdentifier: 方法并在那里加载笔尖。将自定义视图添加到单元格的内容视图中:
然后在
cellForRowAtIndexPath
代码中,您只需调用:To make a custom cell create a subclass of UITableViewCell. Make a nib with a view and connect an outlet to it:
Override initWithStyle: reuseIdentifier: method and load the nib there. Add the custom view to the cell's content view:
Then in the
cellForRowAtIndexPath
code you simply call:由于我的 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:
Instead of
Hope that helps someone else too..