0

Я пытаюсь создать метод, который изменяет строковый объект «tableColorName» на выбранную ячейку. Таблица Data NSArray состоит из объекта: «красный», «синий», «зеленый». Я хочу сохранить строку «tableColorName» до redColor, если выбран красный, blueColor если синий, greenColor если зеленый. После выбора ячейки я хочу, чтобы viewController возвращался к корню. Я ценю вашу помощь заранее:Выбор ячейки в таблице IOS

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath  *)indexPath 
{ 
    int theRow = indexPath.row; 
    NSString *tableColorName; 
    tableColorName = [[NSString alloc] initWithString:([_tableData [theRow]  stringValue],@"Color")]; 
    [self.navigationController popToRootViewControllerAnimated:YES]; 
} 
+1

Вы можете показать нам метод cellForRowAtIndexPath –

ответ

1

Попробуйте ::

NSArray *arr; 
NSString *tableColorName; // Use in AppDelegate 

- (void)viewDidLoad 
{ 
    arr = [[NSArray alloc] initWithObjects:@"Red", @"Green", @"Blue", nil]; 
} 

Таблица Список Методы ::

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell.title.text = [NSString stringWithFormat:@"%@", [arr objectAtIndex:indexPath.row]]; 
    return cell; 
} 

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    app.tableColorName = [NSString StringWithFormat:@"%@ Color", [arr objectAtIndex:indexPath.row]]; 
    [self.navigationController popToRootViewControllerAnimated:YES]; 
} 

Затем, доступ app.tableColorName когда вы хотите отобразить.

Спасибо.

+0

благодарит за вашу помощь. Мне не нужно было использовать приложение. префикс ... работает как шарм – ShadyBaker

0
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    //do whatever with the selected cell. 
    //go back to the root 
} 
2
//first of all take one NSArray and 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
    self.colorNames = [[NSArray alloc] initWithObjects:@"Red", @"Green", 
        @"Blue", @"Indigo", @"Violet", nil]; 

} 

// Implement Table method 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  { 
    return [self.colorNames count]; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell. 
    [email protected]"Colors"; 

    UIImage *cellImage = [UIImage imageNamed:@"a.png"]; 
    cell.imageView.image = cellImage; 

    NSString *colorString = [self.colorNames objectAtIndex: [indexPath row]]; 

    cell.textLabel.text = colorString; 

    NSString *subtitle = [NSString stringWithString: @"All about the color "]; 
    subtitle = [subtitle stringByAppendingFormat:colorString]; 

    cell.detailTextLabel.text = subtitle; 

    return cell; 
} 

- (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath  *)indexPath 
{ 
    int idx = indexPath.row; 
    obj.lbl.text=[@"You select "stringByAppendingString:[colorNames objectAtIndex:idx]]; 

    [self popToViewController animated:YES]; 
} 
+0

Большое спасибо. очень полезно – ShadyBaker

Смежные вопросы