2013-03-15 81 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