iOS,如何找到 UITableView 的选定行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18637332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS, How to find selected row Of UITableView?
提问by Pratap Manish Bhadoria
I'm trying to find selected UITableViewCell
as following:
我试图找到UITableViewCell
如下选择:
- (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];
}
if ([indexPath row] == [tableView cellForRowAtIndexPath:indexPath.row]) // i want to place value of selected row to populate the buttons
//([indexPath row] == 0)
//(indexPath.row == ![self cellIsSelected:indexPath])
{
UIButton *AddComment = [UIButton buttonWithType:UIButtonTypeCustom]; // custom means transparent it takes shape same with backgroup image
[AddComment addTarget:self
action:@selector(TestBtns:)
forControlEvents:UIControlEventTouchDown];
// [AddComment setTitle:@"1" forState:UIControlStateNormal];
AddComment.frame = CGRectMake(9.0, 128.0, 96.0, 26.0);
[cell.contentView addSubview:AddComment];
UIImage *buttonAddComment = [UIImage imageNamed:@"addcomment.png"];
[AddComment setBackgroundImage:buttonAddComment forState:UIControlStateNormal];
[cell.contentView addSubview:AddComment];
}
How to achieve this, can you please guide me, where I'm doing Mistake.
如何实现这一点,请您指导我,我在哪里做错了。
回答by Suraj K Thomas
Objective C
目标 C
NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];
Swift code
SWIFT代码
let indexPathForSelectedRow = self.tableView.indexPathForSelectedRow
回答by iPatel
Use this delegatemethod of UITableView
使用这个委托方法UITableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%d", indexPath.row); // you can see selected row number in your console;
}
回答by Vaibhav Gautam
in UITableViewDataSource delegate there is a method - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath.
在 UITableViewDataSource 委托中有一个方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath。
It returns NSIndexPath object which caontains both the selected section and selected row.
它返回包含选定部分和选定行的 NSIndexPath 对象。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"Selected section>> %d",indexPath.section);
NSLog(@"Selected row of section >> %d",indexPath.row);
}
make sure to set datasource of tableview before using it otherwise this method won't be called
确保在使用之前设置 tableview 的数据源,否则将不会调用此方法
回答by salaourn
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int a = indexPath.row;
NSLog(@"index: %i",a);
}
回答by Mini
I think what you are trying to do is not to get the index of the table view cell clicked but to know which cell is clicked.
我认为您要做的不是获取单击的表视图单元格的索引,而是要知道单击了哪个单元格。
To know this at the time of assigning each cell assign the indexpath.rowvalue as the tag of the cell created then on clicked take the table view as the parent and try to get its subview(cell) with that tag
要在分配每个单元格时知道这一点,将indexpath.row值指定为创建的单元格的标签,然后单击将表视图作为父视图,并尝试使用该标签获取其子视图(单元格)
[Tableview view with tag:indexpath.row]
[带有标签的 Tableview 视图:indexpath.row]
回答by CodeLover
Here's the built-in method that will help you determine which cell has been selected.
这是帮助您确定已选择哪个单元格的内置方法。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// you can use "indexPath" to know what cell has been selected as the following
NSLog(@"Selected row is %@", indexPath);
}
and by the way the method is already given when you create TableViewController, you probably just have to uncomment it.
顺便说一下,在您创建 TableViewController 时已经给出了该方法,您可能只需要取消对它的注释。
Hope you find it helpful
希望你觉得它有帮助