[英]Button inside a custom uitableviewcell not working
我有一個自定義的TableCell,UiButton通過IB與此相關聯。 但是按鈕操作方法總是將按鈕索引返回為零
以下是我的代碼
//Inside CellForRow
[cell.M_CtrlBtnChat addTarget: self
action: @selector(buttonPressed:withEvent:)
forControlEvents: UIControlEventTouchUpInside];
- (void) buttonPressed: (id) sender withEvent: (UIEvent *) event
{
UITouch * touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView: self.tableView];
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: location];
NSLog(@"%d", indexPath.row);
}
索引路徑始終返回零。 任何想法 。 請幫我
[cell.M_CtrlBtnChat addTarget: self
action: @selector(buttonPressed:withEvent:)
forControlEvents: UIControlEventTouchUpInside];
cell.M_CtrlBtnChat.tag = indexPath.row;
- (void) buttonPressed: (id) sender withEvent: (UIEvent *) event
{
UIButton *btn = (UIButton *)sender;
NSLog(@"btn.tag %d",btn.tag);
UITouch * touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView: self.tableView];
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: location];
NSLog(@"%d", indexPath.row);
}
試試這可能會對你有所幫助。
我有同樣的問題,並嘗試了一切。 最終做到的是實現該方法
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
只需返回自定義單元格的高度(查看NIB文件)
- (void) buttonPressed: (id) sender withEvent: (UIEvent *) event
{
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:self.mMessagesTable];
NSIndexPath *indexPath = [self.mMessagesTable indexPathForRowAtPoint:touchPoint];
NSLog(@"%d", indexPath.row);
}
以編程方式的按鈕代碼
UIButton *mNewMsgDwn = [UIButton buttonWithType:UIButtonTypeCustom];
[mNewMsgDwn setImage:[UIImage imageNamed:@"new_message.png"] forState:UIControlStateNormal];
[mNewMsgDwn addTarget:self action:@selector(newMsg) forControlEvents:UIControlEventTouchUpInside];
mNewMsgDwn.frame = CGRectMake(179, 357, 137, 27);
[self.view addSubview:mNewMsgDwn];
嘗試使用這個波紋管代碼..
- (void) buttonPressed: (id) sender withEvent: (UIEvent *) event
{
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)button.superview;
UITableView *tableView = (UITableView *)cell.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSLog(@"%d", indexPath.row);
}
要么
UIButton *button = (UIButton *)sender;
CGRect buttonFrame = [button convertRect:button.bounds toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonFrame.origin];
或整個示例,以編程方式在單元格中添加自定義按鈕
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
.......
cell.textLabel.text = [NSString stringWithFormat:@"Cell #%i", indexPath.row + 1];
//Create the button and add it to the cell
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Custom Action" forState:UIControlStateNormal];
button.frame = CGRectMake(150.0f, 5.0f, 150.0f, 30.0f);
[cell addSubview:button];
return cell;
}
並使用波紋管代碼獲取索引路徑...
//Get the superview from this button which will be our cell
UITableViewCell *tblCell = (UITableViewCell*)[sender superview];
//From the cell get its index path.
NSIndexPath *indexPathE = [myTableView indexPathForCell:tblCell];
NSLog(@"%d", indexPathE.row);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.