xcode 如何将表格中的单元格文本转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5260353/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:25:56  来源:igfitidea点击:

how to get text of cell from table in to string

iphoneobjective-cxcodeuitableviewtitle

提问by Pooja

i have 2 views and both are table views 1st view list is there, so when user click on particular cell, i want to take the text of selected cell and store into variable and push 2nd view

我有 2 个视图,并且都是表视图 第一个视图列表在那里,所以当用户单击特定单元格时,我想获取所选单元格的文本并存储到变量中并推送第二个视图

e.g. stringVariable = cell.text

例如 stringVariable = cell.text

2nd view

第二视图

now i want to set title of view by using stringVariable variable

现在我想使用 stringVariable 变量设置视图标题

e.g. self.title = stringVariable;

例如 self.title = stringVariable;

i hope some one know this issue

我希望有人知道这个问题

thank you in advance

先感谢您

回答by Manni

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Save text of the selected cell:
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    stringVariable = cell.textLabel.text;

    // load next view and set title:
    MyView *nextView = [[MyView alloc] initWithNibName:@"MyView" bundle:nil];
    nextView.title = stringVariable;
    [self.navigationController pushViewController:nextView animated:YES];           

}

回答by Vaibhav Tekam

You must be using some array data to fill the table Views.

您必须使用一些数组数据来填充表视图。

eg.

例如。

- (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];
    }
    TableItem * tableItem = [tableItems objectAtIndex:indexPath.row];
    UILabel mainLabel = [[UILable alloc] initWithFrame:()];
    mainLabel.text = tableItem.name;
    [cell.contentView addSubView:mainLabel];
    [mainLabel release];

    return cell;
}

Similarly you should use the same array to get the required item, which is selected

同样,您应该使用相同的数组来获取所需的项目,该项目被选中

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        TableItem * tableItem = [tableItems objectAtIndex:indexPath.row];
        OtherViewController * otherViewController = [[OtherViewController alloc] init];
        otherViewController.name = tableItem.name;
        [self.navigationController pushViewController:otherViewController animated:YES]; 
        [otherViewController release];
}