我將UITableView
放到UIView
上,其中包含我的UIScrollView
。我已將代理和數據源設置爲我的UIViewController
,但我注意到tableView:didSelectRowAtIndexPath
方法未被調用。我沒有touchesBegan
或任何其他觸摸*方法重寫。我在這裏嘗試了第4個答案:UIScrollView touchesBegan繼承我的UIScrollView
,但該方法仍然未被調用。表格單元格已正確填充。爲什麼tableView:didSelectRowAtIndexPath被包含在UIScrollView中的UITableView被調用?
回答
不要在UIScrollView上實現任何UITapGestureRecognizer。或touchesBegin在UIScrollView類。它會工作。
啊,沒有意識到輕拍手勢識別器將是罪魁禍首。 Sooooo,因爲我實際上需要這個水龍頭手勢識別器,有沒有辦法讓他們都能工作? – Architekt
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch :(UITouch *)touch { return![NSStringFromClass([touch.view class])isEqualToString:@「UITableViewCellContentView」]; } – Xeieshan
我完全看着那個想法。感謝您指出了這一點。 – GoGreen
我是否正確理解UITableView
是UIView
的子視圖,並且UIView
是UIScrollView
的子視圖?
爲什麼它的價值,這在我的iOS 7模擬器(我收到-tableView:didSelectRowAtIndexPath:
消息)罰款。這裏有一個視圖控制器,可以用來測試我的實現。
@interface ContainedScrollersViewController() <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIView *tableViewContainer;
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *values;
@end
@implementation ContainedScrollersViewController
- (id)init
{
self = [super init];
if (self) {
int countValues = 20;
_values = [[NSMutableArray alloc] initWithCapacity:countValues];
for (int i = 0; i < countValues; i++) {
[_values addObject:@(arc4random() % 100)];
}
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_scrollView = [[UIScrollView alloc] init];
_scrollView.backgroundColor = [UIColor grayColor];
_tableViewContainer = [[UIView alloc] init];
_tableViewContainer.backgroundColor = [UIColor lightGrayColor];
_tableView = [[UITableView alloc] init];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_scrollView];
[_scrollView addSubview:_tableViewContainer];
[_tableViewContainer addSubview:_tableView];
}
- (void)viewDidLayoutSubviews
{
self.scrollView.frame = self.view.bounds;
// arbitrary sizes to visualize each view in the hierarchy
self.scrollView.contentSize = CGSizeMake(640.0f, 960.0f);
self.tableViewContainer.frame = CGRectMake(0.0f, 0.0f, 400.0f, 600.0f);
self.tableView.frame = self.view.bounds;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.values.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@", [self.values objectAtIndex:indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"selected row:%i", indexPath.row);
}
@end
- 1. 爲什麼UITableView numberOfRowsInSection被多次調用?
- 2. 在UIScrollview的UIView中的UITableView:在UITableView中的數據被清除
- 3. 爲什麼Dispose被調用?
- 4. 爲什麼鹽被包含在用C crypt函數哈希
- 5. 爲什麼margin-top在這裏被應用到包含元素?
- 6. 爲什麼包含在EF6中被忽略
- 7. 監視什麼軟件包被調用
- 8. 爲什麼不在@Before被調用
- 9. 爲什麼PK被列爲其索引中包含列?
- 10. 爲什麼回調函數被調用?
- 11. 爲什麼「錯誤:」回調被調用
- 12. 爲什麼UITableView調用UIScrollView的委託方法?
- 13. 爲什麼我的PreInvocationAuthorizationAdvice.before未被調用?
- 14. 爲什麼錯誤的ctor被調用?
- 15. 爲什麼我的TryInvokeMember未被調用?
- 16. 爲什麼onActivityResult()在調用它的人爲null時被調用?
- 17. viewForHeaderInSection在UITableView中僅被調用一次
- 18. 爲什麼我的函數在不應該被調用時被調用?
- 19. 爲什麼我DEBUG_PRINT宏不被調用的,包括它
- 20. willTransitionToState沒有被調用(UITableview)
- 21. UITableView RowSelected沒有被調用?
- 22. numberOfRowsInSection沒有被調用UITableView
- 23. UITableView tableView:cellForRowAtIndexPath:沒有被調用
- 24. UITableView titleForHeaderInSection未被調用
- 25. UITableView cellForRowAtIndexPath沒有被調用
- 26. scrollViewDidEndZooming在放大UIScrollView時未被調用
- 27. 爲什麼OnlyOnCanceled延續被調用?
- 28. 爲什麼OnActivate不被調用?
- 29. 爲什麼CustomeMembershipProvider沒有被調用?
- 30. 爲什麼不__dict__可以被調用
發表一些關於如何添加tableview的代碼? – johnMa
檢查uitableView的用戶交互。它應該被啓用? –