我使用UITableviewcellEditingstyleDelete
显示用户点击它的按钮以显示删除按钮(这与您在电子邮件应用中看到的方式相同,当用户单击编辑然后单击按钮以显示删除按钮时)。它在ios6中工作正常但是当我在具有ios 7的设备上构建我的应用程序时,删除按钮消失了,但是当你点击删除按钮的区域时它也可以删除。问题是用户无法看到删除按钮(操作系统提供的红色按钮)。
我的代码是:
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Detemine if it's in editing mode
if (self.editing)
{
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}
请帮我找到解决方案,我对iOS7环境了解不多。谢谢!
答案 0 :(得分:2)
它看起来像iOS 7
中的一个Bug。出于某种原因,backgroundView
被iOS
移动到删除按钮上。您可以通过对backgroundView
进行子类化并实现派生视图的setFrame
函数来解决此问题,如下所示:
UITableViewCell delete button gets covered up.
指定accesoryView
且editingAccessoryView
为nil
时也可能发生这种情况。这里提到了这个问题和解决方案的详细解释:
UITableViewCell content overlaps delete button when in editing mode in iOS7.
答案 1 :(得分:2)
感谢大家的建议,这个解决方案可以解决它,我把它变成自定义单元格
UIImageView* backgroundImage;
//Variable for animation delete button
// Keep the ContactView in normal state
BOOL bFirstEditingCell;
BOOL bShownRedMinus;
BOOL bShownDeleteButton;
CGRect frameOfContactViewInNormal;
CGPoint centerOfCellInNormal;
UITableViewCellAccessoryType accessoryTypeInNormal;
//
- (id)initWithCoder:(NSCoder *)decoder
{
if (self = [super initWithCoder:decoder])
{
super.backgroundColor = [UIColor clearColor];
self.selectionStyle = UITableViewCellSelectionStyleNone;
backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"row_gradient" ]];
self.backgroundView = backgroundImage;
bShownDeleteButton = false;
bShownRedMinus = false;
bFirstEditingCell = true;
accessoryTypeInNormal = UITableViewCellAccessoryNone;
}
return self;
}
- (void)willTransitionToState:(UITableViewCellStateMask)state {
[super willTransitionToState:state];
if (!isOS7()) {
return;
}
for (UIView *subview in self.subviews) {
//Keep normal value of contact view and cell
if (bFirstEditingCell ) {
frameOfContactViewInNormal = self->contactView.frame;
frameOfContactViewInNormal.size.width = kContactViewWidth;
centerOfCellInNormal = subview.center;
bFirstEditingCell = false;
}
if (state == UITableViewCellStateDefaultMask) {
self.backgroundView = backgroundImage;
subview.center = centerOfCellInNormal;
//Set for position of speed dial image
CGRect f = frameOfContactViewInNormal;
if (bShownRedMinus) {
f.size.width -= kRedMinusButtonWidth;
}
self->contactView.frame = f;
bShownDeleteButton = false;
self.accessoryType = accessoryTypeInNormal;
}
else if (state == UITableViewCellStateShowingDeleteConfirmationMask)
{
float sectionIndexWidth = 0.0;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
sectionIndexWidth = 30.0;
}
else {
sectionIndexWidth = 15.0;
}
CGPoint center = centerOfCellInNormal;
subview.center = CGPointMake(center.x - sectionIndexWidth, center.y);
self.backgroundView = nil;
//Set width of contact name
UIView* view = [subview.subviews objectAtIndex: 0];
CGRect f = view.frame;
f.origin.x = (kDeleteButtonWidth + sectionIndexWidth);
view.frame = f;
f = frameOfContactViewInNormal;
f.size.width = self.frame.size.width - (kDeleteButtonWidth + sectionIndexWidth);
self->contactView.frame = f;
bShownDeleteButton = true;
bShownRedMinus = false;
accessoryTypeInNormal = self.accessoryType;
self.accessoryType = UITableViewCellAccessoryNone;
}
else if (state == UITableViewCellStateShowingEditControlMask) {
CGRect f = frameOfContactViewInNormal;
f.size.width -= 5;
self->contactView.frame = f;
bShownRedMinus = true;
}
else if (state == 3) { //State for clicking red minus button
CGRect f = frameOfContactViewInNormal;
f.size.width += kRedMinusButtonWidth;
self->contactView.frame = f;
self.backgroundView = nil;
}
}
}
答案 2 :(得分:1)
您无需检查是否正在编辑tableView ...只需实现:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
答案 3 :(得分:0)
解决此问题的最佳方法是在单元格中添加图像并将其设置在Backside中。
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bgImg.png"]];
imageView.frame = CGRectMake(0, 0, 320, yourCustomCell.frame.size.height);
[yourCustomCell addSubview:imageView];
[yourCustomCell sendSubviewToBack:imageView];
如果您的文本与删除按钮重叠,则执行Autolayout。它会以更好的方式管理它。
可以生成另一个案例,即cellSelectionStyle将使用默认颜色突出显示。您可以按如下方式设置高亮颜色
yourCustomCell.selectionStyle = UITableViewCellSelectionStyleNone;
Set your table cell's selection style to UITableViewCellSelectionStyleNone. This will remove the blue background highlighting or other. Then, to make the text label or contentview highlighting work the way you want, use this method in yourCustomCell.m class.
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
if (highlighted)
self.contentView.backgroundColor = [UIColor greenColor];
else
self.contentView.backgroundColor = [UIColor clearColor];
}
我希望它能帮助你理解。
答案 4 :(得分:0)
假设一个仅限iOS7的应用程序,其技术类似于上面Vin发布的链接,我相信这里的方法:https://stackoverflow.com/a/19416870/535054会更清晰。
在这种方法中,您不需要对backgroundView进行子类化,对于不同的单元格可能会有所不同。
将代码放在我上面链接的答案中,在自定义表格单元格层次结构的根目录中,以及所有表格单元格(从中继承),只要他们使用backgroundView或selectedBackgroundView属性就可以获得修复
从此要点复制解决方案:https://gist.github.com/idStar/7018104
答案 5 :(得分:0)
解决此问题的一种简单方法是将delete confirmation button view
作为前视图。可以通过在layoutSubviews
文件中实现委托方法customtableviewcell.m
来完成。
在我的情况下,我通过将以下代码添加到customtableviewcell.m
文件中来解决它。根据视图放置在您的单元格中的方式,可能会略有不同。但肯定会让你知道如何解决这个问题。
- (void)layoutSubviews // called when a interface orientation occur ie. in our case when the '-' button clicks
{
[super layoutSubviews];
for (UIView *subview in self.subviews) { // Loop through all the main views of cell
// Check the view is DeleteConfirmationView or not, if yes bring it into the front
if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
[self bringSubviewToFront:subview];// code for bring the view into the front of all subviews
}
}
}