gpt4 book ai didi

ios - 在 UITableView 编辑模式下,UITableView :didSelectRowAtIndexPath: doesn't respond

转载 作者:行者123 更新时间:2023-11-28 22:39:47 24 4
gpt4 key购买 nike

我正在开发一个 iOS 应用程序,我正在尝试使用自定义 UITableViewCell 实现我自己的自定义 UITableView 编辑模式。

我有一个编辑按钮,这是它的 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 是否处于编辑模式有关。

最佳答案

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

enter image description here

关于ios - 在 UITableView 编辑模式下,UITableView :didSelectRowAtIndexPath: doesn't respond,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14892445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com