如何在UITableView中保存用户选择的字符串

时间:2014-08-17 02:00:34

标签: ios ios7

我希望将所选的字符串(由附件复选标记表示)保存到要使用的字符串中并显示在不同的视图控制器中。作为一个例子,我在我的数组中有这些不同的对象" Business"," Computer Science"等。在下一个视图控制器中,我希望我的空标签显​​示用户具有的字符串地选择。

我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.array = [[NSArray alloc]initWithObjects:@"Business",@"Computer Science", @"Economics", @"Mathematics",nil];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma Table View Methods

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    cell.textLabel.text = [self.array objectAtIndex:indexPath.row];

    return cell;
}

@end

1 个答案:

答案 0 :(得分:0)

你可以这样试试

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"%@",cell.textLabel.text);

}