我正在开发一个iOS应用程序,我正在尝试使用自定义UITableView
实现我自己的自定义UITableViewCell
编辑模式。
我有一个编辑按钮,这是它的IBAction:
- (IBAction)editFavList:(id)sender
{
if ([_favList isEditing])
{
[_favList setEditing:NO animated:YES];
[_editButton setTitle:@"Edit" forState:UIControlStateNormal];
}
else
{
[_favList setEditing:YES animated:YES];
[_editButton setTitle:@"Done" forState:UIControlStateNormal];
}
}
我已将UITableView* _favList
delegate
与UIViewController
联系起来,这个UITableViewDelegate
方法正常工作,直到我点击编辑按钮:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.editing)
{
NSNumber* obj = [favsSelected objectAtIndex:indexPath.row];
BOOL selected;
if (obj == nil)
selected = NO;
else
selected = [obj boolValue];
FavouriteCell* cell =
(FavouriteCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.checked = !selected;
// Actualizo el estado en el vector de los favoritos seleccionados
[favsSelected insertObject:[NSNumber numberWithBool:!selected] atIndex:indexPath.row];
}
}
点击编辑按钮后,此方法不会触发(我很确定,因为我在方法上添加断点)。
这是自定义单元格实现:
#import "FavouriteCell.h"
const NSInteger EDITING_HORIZONTAL_OFFSET = 35;
@implementation FavouriteCell
@synthesize selectIcon = _selectIcon;
@synthesize favName = _favName;
@synthesize checked = _checked;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.checked = NO;
}
return self;
}
- (void)setChecked:(BOOL)checked
{
if (checked == _checked)
return;
_selectIcon.highlighted = checked;
_checked = checked;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
+ (NSString *)reuseIdentifier
{
return @"favouriteCell";
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
//self.editing = editing;
[super setNeedsLayout];
}
#pragma mark - Private methods
- (void)layoutSubviews
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[super layoutSubviews];
if (((UITableView *)self.superview).isEditing)
{
CGRect contentFrame = self.contentView.frame;
contentFrame.origin.x = EDITING_HORIZONTAL_OFFSET;
self.contentView.frame = contentFrame;
self.accessoryType = UITableViewCellAccessoryNone;
}
else
{
CGRect contentFrame = self.contentView.frame;
contentFrame.origin.x = 0;
self.contentView.frame = contentFrame;
self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
[UIView commitAnimations];
}
@end
但是,如果我再次点击编辑按钮(然后,我退出编辑模式),如果我点击一行,didSelectRowAtIndexPath
它会再次触发。
为什么我做错了?可能这个问题与UITableView
是否处于编辑模式有关。
答案 0 :(得分:1)
您应该将UITableView的allowsSelectionDuringEditing
属性设置为YES
在你的情况下:
_favList.allowSelectionDuringEditing = YES;
答案 1 :(得分:1)
检查nib文件。您应该将tableView editing
属性更改为Single Selection during editing
。