2013-03-15 104 views
0

我想創建一個方法,將字符串對象「tableColorName」更改爲所選單元格。 tableData 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'方法 – 2013-03-15 03:17:02

回答

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 2013-03-16 09:10:20

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 2013-03-16 09:08:44