我是 iOS 开发新手,我想知道当我单击表格行时,表格 View 中显示的数据应该在标签上选择。我的标签名为 City。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [arrayname objectAtIndex:indexPath.row];
return cell;
}
最佳答案
在 viewDidLoad 中添加这个数组
dataArr=[[NSMutableArray alloc]initWithObjects:@"Hyderabad",@"Bangalore",@"Chennai",@"Pune",@"Mumbai", nil];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *reUseid=@"cellID";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reUseid];
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reUseid];
}
cell.textLabel.text = dataArr[indexPath.row];
return cell;
}
这里是 TableView didSelectRowAtIndexPath 方法。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"selected is %@",[dataArr objectAtIndex:indexPath.row]);
[self performSegueWithIdentifier:@"viewToDescriptionSegue" sender:self];
}
在这个方法中你得到了indexpath
,你可以从中得到选定的行,比如indexpath.row
,你可以从中得到数组中的对象。
关于ios - TableView 标签上显示的 TableView 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32990747/