我有一个带UITableView的视图控制器。我将数据源和委托都设置为视图控制器。正在调用方法cellForRowAtIndexPath:并按预期运行,但是没有调用didSelectRowAtIndexPath:。有什么想法在这里发生吗?
我的.h:
@interface DetailViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
在.m中我的viewDidLoad中我有:
self.tableView.dataSource=self;
self.tableView.delegate=self;
似乎有意义的是调用cellForRowAtIndexPath而不是didSelectRowAtIndexPath。这是该方法现在的样子:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"did select row at index path");
}
答案 0 :(得分:3)
我遇到了一些可能的原因:
确保tableView的编辑属性设置为NO。当table的编辑属性设置为YES时,不会调用 didSelectRowAtIndex:method 。默认情况下,此属性设置为NO。
self.tableView.editing = NO;
确保allowSelection属性设置为YES。默认情况下是。
尝试在viewDidLoad方法中调用。 [super viewDidLoad] ,或在init初始化程序中设置委托和dataSource属性。
在 loadView 方法中设置委托和dataSource属性,并将tableView分配给UIViewController的view属性。这是一个例子:
- (void)loadView
{
UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
tableView.delegate = self;
tableView.dataSource = self;
[tableView reloadData];
self.view = tableView;
}
(实际上,这是我通常做的事情)尝试从UITableViewController继承DetailViewController,它继承自UIViewController。 UITableViewController自动编译为UITableViewDelegate和UITableViewDataSource。
答案 1 :(得分:1)
将答案放在seto nugroho的评论中并解决问题。如果tableView中有手势识别器,则不会调用didSelect行。您可以做的是将手势识别器的水龙头数量增加到两个。
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
gestureRecognizer.numberOfTapsRequired = 2
view.addGestureRecognizer(gestureRecognizer)`
答案 2 :(得分:1)
视图控制器视图上的手势识别器可以防止触摸,方法是将手势识别器的cancelsTouchesInView
设置为false。
答案 3 :(得分:0)
就我而言,我使用了UITapGestureRecognizer,它对我来说就像是魔术。
cell.tag=indexPath.row;
UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapOnCell:)];
[cell addGestureRecognizer:tap];
这是我的tapOnCell方法实现
-(void)tapOnCell:(UITapGestureRecognizer*)tap
{
ADPointCashTransaction* aTransaction=[transactionArray objectAtIndex:tap.view.tag];
// Work with aTransaction Object
}
答案 4 :(得分:0)
我将再添加一个:确保您在任何时候都没有任何其他视图与表或单元格重叠,即使是隐藏的视图也是如此。如果这样做,除非您明确需要通过视图捕捉手势,否则请在情节提要中禁用“已启用用户交互”。
答案 5 :(得分:0)
就我而言,我正在分配dataSource并在后台线程中全部调用reloadData()。因此,请确保您正在主线程上执行此操作。