ios - 在表 IOS 中选择单元格

标签 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];
}

最佳答案

//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]; 
}

关于ios - 在表 IOS 中选择单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15424043/

相关文章:

ios - UICollectionView 选择和取消选择问题

iphone - 为什么这会导致内存泄漏?

ios - 在 ipad 应用程序中将 wikitext 保存到服务器

objective-c - 当这个 NSDictionary 调用 initWithArray 时,为什么它包含 0 个对象?

ios - 为什么我的 NSDateFormatter 创建了错误的日期?

iOS UI 测试 : Handle all system prompt automatically with addUIInterruptionMonitorWithDescription

iphone - 核心数据从数组中选择对象 - iPhone

objective-c - 标准(32/64 位通用)、WebKit、Flash 插件和 Leopard

ios - 无法通过 UI 导航 Controller 传递数据 - iOS

ios - 将扫描的条码值解码为int值