如何获取在UITableview中选择的行的标题 [英] how to get title of of row selected in UITableview

查看:109
本文介绍了如何获取在UITableview中选择的行的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有每个单元格上有一些名称的tableview,如何在选择行时获得该名称?

I have tableview with some names on each cell , how can i get that name when select row ?

我知道我必须使用委托方法

i know that i have to use delegate method

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

但是我怎么能得到那一行的标题?

but how can i get that title of that row ?

提前感谢

问候

推荐答案

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *cellText = selectedCell.textLabel.text;
}

此代码段检索所选单元格并存储其文本 NSString 中的属性。

This snippet retrieves the selected cell and stores its text property in a NSString.

但是,我不推荐这个。最好将数据和表示层分开。使用后备数据存储(例如数组)并使用传递的 indexPath 对象中的数据(例如,用于访问阵列的索引)访问它。此数据存储将与用于填充表格的数据存储相同。

However, I do not recommend this. It is better to keep the data and presentation layers separate. Use a backing data store (e.g. an array) and access it using the data in the passed indexPath object (e.g. the index to use to access the array). This data store will be the same one used to populate the table.

这篇关于如何获取在UITableview中选择的行的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆