ios 如何从 iPhone 的表格视图中获取选定单元格的单元格值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14562551/
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
how to get the cell value of a selected cell from Table view in iPhone
提问by Sabarish
Am displaying images in a table view controller where the images are rendered from the URL as XML file. It works well with listing images as scroll view. Now i want to select a particular image and the window should show the selected Cell images alone. For this do i need to get the Cell value. If so how can i get the particular cell value and display its corresponding images as a collection view in next window.
我在表视图控制器中显示图像,其中图像从 URL 呈现为 XML 文件。它适用于将图像列为滚动视图。现在我想选择一个特定的图像,窗口应该单独显示选定的单元格图像。为此,我需要获取 Cell 值。如果是这样,我如何获取特定的单元格值并在下一个窗口中将其相应的图像显示为集合视图。
Kindly suggest me an idea. For your better understanding i pasted below an image how my storyboard currently looks and how i need an output.
请给我一个想法。为了您更好地理解,我在图片下方粘贴了我的故事板当前的外观以及我需要输出的方式。
Hope you understand my problem.
希望你明白我的问题。
回答by Vinayak Kini
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// here we get the cell from the selected row.
UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];
// perform required operation
UIImage * image =selectedCell.image;
// use the image in the next window using view controller instance of that view.
}
回答by Aditya Mathur
You should use UITableViewDelegate
你应该使用 UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
indexPath will return you number of section and number of row of selected row.
indexPath 将返回所选行的节数和行数。
indexPath.section;
indexPath.row
I hope it was clear. For further understanding you may refer to following tutorials: http://www.edumobile.org/iphone/iphone-programming-tutorials/using-iphone-tableview-for-displaying-data/http://www.raywenderlich.com/1797/how-to-create-a-simple-iphone-app-tutorial-part-1
我希望很清楚。为了进一步了解您可以参考以下教程:http: //www.edumobile.org/iphone/iphone-programming-tutorials/using-iphone-tableview-for-displaying-data/ http://www.raywenderlich.com/ 1797/how-to-create-a-simple-iphone-app-tutorial-part-1
回答by Nameet
In this case I think you should maintain id for each image on the row. It could be your row number, if you have one image per row. Then from
在这种情况下,我认为您应该为行中的每个图像维护 id。如果每行有一张图像,它可能是您的行号。然后从
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
you can pass the id to the next view controller. In this view controller you should use this id to get the required data to create your collection view. Hope this helps.
您可以将 id 传递给下一个视图控制器。在此视图控制器中,您应该使用此 ID 来获取创建集合视图所需的数据。希望这可以帮助。
回答by Narasimha Nallamsetty
For me this is working..
对我来说这是工作..
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
customerVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"customerInfoView"];
customerVC.selectedText = cell.textLabel.text;
//customerVC is destination viewController instance)
[self.navigationController pushViewController:customerVC animated:YES];
}
And in your destination view controller header file just declare a string like
在您的目标视图控制器头文件中,只需声明一个字符串
@property NSString *selectedText;
In viewcontroller.m assign the value like
在 viewcontroller.m 中分配如下值
self.selectedBiller.text = self.selectedText;
(selectedBiller is a label here)
(selectedBiller 是这里的一个标签)