繁体   English   中英

在表IOS中选择单元格

[英]Selecting Cell in Table IOS

我正在尝试创建一种将字符串对象“ tableColorName”更改为所选单元格的方法。 tableData NSArray由对象组成:“红色”,“蓝色”,“绿色”。 我想将字符串“ 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];
}
//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]; 
}

尝试这个 ::

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访问。

谢谢。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM