我有一个tableview有几个自定义单元格。第一个是一个按钮。当按下此按钮时,我想滚动到我的tableview中的某个部分。我该如何将按钮动作与tableview连接起来?scrollToRowAtIndexPath当按下里面的UITableViewCell按钮
1
A
回答
1
你可以让你的电池的按钮属性,并在cellForRowAtIndexPath
可以设置目标加载表中的类查看,所以你不需要任何代表。这样的事情:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"YourCellIdentifier";
YourCustomCell *cell =[tableView dequeueReusableCellWithIdentifier:identifier];
if(cell == nil) {
cell = [YourCustomCell alloc/init method....
[cell.buttonProperty addTarget:self action:@selector(cellButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
}
//do other stuff with your cell
}
-(void)cellButtonTapped:(id)sender {
UIButton *button = (UIButton*)sender;
YourCustomCell *cell = (YourCustomCell*)button.superview.superview; //if the button is added to cell contentView or button.superview is added directly to the cell
NSIndexPath *path = [yourTableView indexPathForCell:cell];
[yourTableView scrollToRowAtIndexPath:path
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
}
1
建立一个委托机制返回从你的单元格 - 当创建单元格时,为它指定一个NSIndexPath,并在点击该按钮时从单元格传回它。
因此,在你的UITableViewCell子类中,你将有:
- (IBAction)buttonPressed:(id)sender
{
[self.delegate buttonPressedOnCellAtIndexPath:cell.indexPath]
}
回到那就是代表控制器,请回覆此法:
- (void)buttonPressedOnCellAtIndexPath:(NSIndexPath)indexPath
{
[self.tableView scrollToRowAtIndexPath:indexPath];
}
2
您可以滚动到某个部分与使用此功能:
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
使用示例为:
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:5 inSection:indexPath.section]
atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
和链接与实现代码如下按钮动作,你可以在你使用协议自定义单元格
相关问题
- 1. 当按钮按下时,找到UITableViewCell中按钮的indexPath?
- 2. 在UITableViewCell里面点击按钮
- 3. 当我按下里面cardview按钮,既cardview项和按钮被点击
- 4. 当按下按钮
- 5. 当按下按钮时,需要访问UITableViewCell的detailTextLabel属性
- 6. 保存按下按钮状态UITableViewCell
- 7. 的JLabel当按下按钮
- 8. 按钮与里面
- 9. 按钮里面的表行TR,当单击按钮时只做按钮动作
- 10. iOS:EXC_BAD_ACCESS当按下按钮
- 11. UITableView当按下按钮
- 12. android:imageview里面的按钮
- 13. UITableViewCell上的按钮
- 14. UItableviewcell中的按钮
- 15. 当我按下按钮时,我的按钮往下掉
- 16. 的if/else里面没有方法按下按钮
- 17. UISearchBar里面取消按钮
- 18. facebook里面像按钮
- 19. 安卓按钮里面PopupWindow
- 20. 按钮下面RecyclerView
- 21. 后面的按钮里面的类大
- 22. PyQt的:当一个按钮被按下
- 23. UITableViewCell中的按钮 - 在prepareForSegue中按下按钮时识别单元格
- 24. 更改iFrame的大小当div里面的iFrame使页面更大按钮按
- 25. 应用按后退按钮当按下后退按钮
- 26. 当按下Asp.net窗体上的任何按钮时也按下默认按钮
- 27. 按下按钮时取得当前页面的信息
- 28. 当键盘按键被按下时,显示按钮被按下
- 29. 当按下按钮,然后开始绘制按钮也复制在面板
- 30. ListView下面的按钮
delegationtain mechanism?有更容易的方法吗?我其实不需要通过任何方法。我只想跳到某个部分。 – DanielR
你能告诉我应该把什么放在我的.h文件中,以便代表团正常工作吗? – DanielR