我正在创建一个基于 TableView 的应用程序。我为表格创建了一个自定义表格单元格,其中包含 2 个标签、1 个图像和 1 个按钮。 TableView 数据源方法工作正常。我将 xib 用于自定义单元格和 View Controller 类,并将委托(delegate)和数据源连接到文件的所有者。但问题是当我选择表行时,didSelectRowAtIndexPath 没有起火。如前所述,启动它的唯一方法是按住电池约 3-4 秒。有谁知道为什么会这样?
感谢任何指点...
这是我的 TableView 方法..
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [finalAddonsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
NewCustomCell *cell = (NewCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"NewCustomCell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
Addons *addons1=[[Addons alloc]init];
addons1= [finalAddonsArray objectAtIndex:indexPath.row];
if (addons1.data == nil) {
cell.ivCategory.image = [UIImage imageNamed:@"blogo.jpg"];
}
else
{
cell.ivCategory.image=[UIImage imageWithData:addons1.data];
}
cell.lblTitle.text = addons1.name;
if (addons1.price == nil) {
cell.lblPrice.text = nil;
}
else{
cell.lblPrice.text = [NSString stringWithFormat:@"%@ rs",addons1.price];
}
[cell.button addTarget:self
action:@selector(editButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
cell.button.tag=indexPath.row;
index = indexPath;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"sjcjksbcjksbcfkebscf1234567890");
}
还有一件事我得到的是,如果我使用默认的 UITableViewCell 而不是自定义单元格,那么我的问题也是一样的,委托(delegate)方法没有被触发。
自定义单元格属性:
最佳答案
同样的问题发生在我身上,因为我在它上面添加了一个点击手势识别器。 如果您使用过任何手势识别器,请尝试将其移除并检查它是否会导致问题。
编辑:Ali 评论的解决方案:
如果您使用了点击手势,您可以使用 [tap setCancelsTouchesInView:NO];
关于ios - iphone-didSelectRowAtIndexPath : only being called after long press on custom cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18159147/