![](/img/trans.png)
[英]didSelectRowAtIndexPath; UITapGestureRecognizer; custom UITableViewCell
[英]Custom UITableViewCell didSelectRowAtIndexPath not being called
我創建了一個自定義UITableViewCell類,用於繪制UITableViewCell。 一切都正確繪制,但是由於要放入UITableViewCell的元素,選擇單元格時遇到了問題。
這是我用來繪制UITableViewCell的方法,這是我的自定義UITableViewCell
- (void)drawCell
{
nameString = [[UILabel alloc] init];
nameString.backgroundColor = [UIColor redColor];
nameString.frame = CGRectMake(15.0, 0.5, 70.0, 40.0);
nameString.text = [itemsDictionary objectForKey:@"Name"];
lastNameString = [[UILabel alloc] init];
lastNameString.backgroundColor = [UIColor clearColor];
lastNameString.frame = CGRectMake(105.0, 0.5, 95.0, 40.0);
lastNameString.text = [itemsDictionary objectForKey:@"LastName"];
addressString = [[UILabel alloc] init];
addressString.backgroundColor = [UIColor clearColor];
addressString.frame = CGRectMake(220.0, 10.5, addressString.frame.size.width, 50.0);
addressString.text = [NSString stringWithFormat:@"ISN %@: %@",[itemsDictionary objectForKey:@"AddNumber"] ,[itemsDictionary objectForKey:@"AddString"]];
[addressString sizeToFit];
// scrollcell has a dynamic scrollwidth depending on the sddressString but has a framesize of a normal UITableViewCell
scrollCell = [[UIScrollView alloc] init];
scrollCell.backgroundColor = [UIColor blueColor];
scrollCell.frame = CGRectMake(0.0, 0.0, ScreenWidth, 45.0);
[scrollCell setContentSize:(CGSizeMake((220.0 + addressString.frame.size.width)+15, 45.0))];
[scrollCell addSubview:nameString];
[scrollCell addSubview:lastNameString];
[scrollCell addSubview:addressString];
[[self contentView] addSubview:scrollCell];
}
如您所見,我正在添加一個UIScrollView,它覆蓋了整個單元格,我認為這阻止了UITableViewCell委托選擇方法。
如何獲得委托方法didSelectRowAtIndexPath起作用?
您是否添加了以下內容?
@implementation ViewController <UITableViewDelegate, UITableViewDataSource>
並將代表設置為自我?
self.tableview.delegate = self;
單元格中的UIScrollView始終會成為問題,因為它會攔截事件。 如果可以,請依靠表格視圖滾動並使該單元格與顯示內容所需的單元格一樣大的事實。
如果必須在其中具有滾動視圖,則可能必須在單元格頂部上方添加一個視圖,添加自己的手勢識別器,然后選擇將哪些事件發送到表格視圖以及將哪些事件發送到滾動視圖。
也請查看此答案 -您還可以將touchesBegan / Moved / Ended發送到nextResponder。
其他兩個答案之一可能是正確的路徑,但以防萬一,如果您使用情節提要劇集,那么這些故事會在didSelectRowAtIndexPath之前得到,因此您所擁有的代碼可能與該劇集和發生的情況無關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.