0

我正在开发一个 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处于编辑模式有关。

4

2 回答 2

1

您应该将:allowsSelectionDuringEditingUITableView 的属性设置为YES

在你的情况下:

_favList.allowSelectionDuringEditing = YES;
于 2013-02-15T10:21:12.697 回答
1

检查 nib 文件。您应该将 tableViewediting属性更改为Single Selection during editing.

在此处输入图像描述

于 2013-02-15T10:22:24.087 回答