在表IOS中选择单元格

时间:2013-03-15 03:12:54

标签: ios objective-c nsstring tableview didselectrowatindexpath

我正在尝试创建一个方法,将字符串对象“tableColorName”更改为所选的单元格。 tableData NSArray由对象组成:“red”,“blue”,“green”。如果选中红色,我想将字符串“tableColorName”保存到redColor,如果是蓝色,则blueColor,如果是绿色则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];
}

3 个答案:

答案 0 :(得分: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]; 
}

答案 1 :(得分: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进行访问。

感谢。

答案 2 :(得分:0)

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

    //do whatever with the selected cell.
    //go back to the root
}