Ale*_*hel 6 iphone uitableview ios
我创建了一个具有自定义UITableViewCell的TableView.每个tableview行都有一个按钮.现在我想知道单击按钮时的行号,以便我知道单击了哪个行按钮.我尝试过在堆栈上找到的一些东西,但没有任何工作.
我试过这段代码 - :
-(void)button1Tapped:(id)sender
{
UIButton *senderButton = (UIButton *)sender;
UITableViewCell *buttonCell = (UITableViewCell *)[senderButton superview];
UITableView* table = (UITableView *)[buttonCell superview];
NSIndexPath* pathOfTheCell = [table indexPathForCell:buttonCell];
NSInteger rowOfTheCell = [pathOfTheCell row];
NSLog(@"rowofthecell %d", rowOfTheCell);
}
Run Code Online (Sandbox Code Playgroud)
但这也行不通.
谢谢你的协助.
SAM*_*HOD 20
尝试这个
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[[cell contentView] setBackgroundColor:[UIColor clearColor]];
[[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
[cell.Mybutton addTarget:self action:@selector(btnCommentClick:) forControlEvents:UIControlEventTouchUpInside];
}
cell.Mybutton.tag=indexPath.row;
}
-(void)btnCommentClick:(id)sender
{
UIButton *senderButton = (UIButton *)sender;
NSLog(@"current Row=%d",senderButton.tag);
NSIndexPath *path = [NSIndexPath indexPathForRow:senderButton.tag inSection:0];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13801 次 |
| 最近记录: |