开发者

how to get text of cell from table in to string

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

e.g. stringVariable = cell.text

2nd view

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

e.g. self.title = stringVariable;

i hope some one know this issue

thank you in advance


- (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];           

}


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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜