0

我正在尝试创建一个将字符串对象“tableColorName”更改为所选单元格的方法。tableDataNSArray由对象组成:“red”、“blue”、“green”。redColor如果选择红色,blueColor如果选择蓝色,如果选择绿色,我想将字符串“tableColorName”保存到greenColor。选择单元格后,我希望 viewController 返回根目录。我提前感谢您的帮助:

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

3 回答 3

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.
    self.navigationItem.title=@"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]; 
}
于 2013-03-15T09:33:56.070 回答
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在您想显示时访问。

谢谢。

于 2013-03-15T09:36:52.010 回答
0
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    //do whatever with the selected cell.
    //go back to the root
}
于 2013-03-15T03:18:29.090 回答