как получить текст ячейки из таблицы в строку - PullRequest
1 голос
/ 10 марта 2011

у меня есть 2 представления, и оба - представления таблицы 1-й вид список есть, поэтому, когда пользователь нажимает на конкретную ячейку, я хочу взять текст выбранной ячейки и сохранить в переменной и нажать 2-й вид

например. stringVariable = cell.text

2-й вид

Теперь я хочу установить заголовок представления, используя переменную stringVariable

например. self.title = stringVariable;

Я надеюсь, что кто-то знает эту проблему

заранее спасибо

Ответы [ 2 ]

7 голосов
/ 10 марта 2011
- (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];           

}
1 голос
/ 10 марта 2011

Вы должны использовать некоторые данные массива для заполнения таблицы Views.

например.

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

Точно так же вы должны использовать тот же массив для получения необходимого элемента, который выбран

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