0

我是 xcode 的新手,正在制作我的第一个 iPhone 应用程序。

UITableview在选项卡式屏幕中有 8 行。我有一个代码,让用户一次最多选择 4 行,并在选择时用检查标记它。

现在,当我更改视图并导航到下一个选项卡时,我想将这些选中行中的文本保存在一个NSString以逗号分隔的变量中。

有可能这样做吗?谢谢,非常感谢您的帮助。

这是我要保存所选行的第一个选项卡的代码。

@implementation Psychological

static int count = 0;
- (void)viewDidLoad {
[super viewDidLoad];

listOfItems = [[NSMutableArray alloc] init];

[listOfItems addObject:@"1 option"];
[listOfItems addObject:@"2 option"];
[listOfItems addObject:@"3 option"];
[listOfItems addObject:@"4 option"];
[listOfItems addObject:@"5 option"];
[listOfItems addObject:@"6 option"];
[listOfItems addObject:@"7 option"];
[listOfItems addObject:@"8 option"];    

}
#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section     {
// Return the number of rows in the section.
return [listOfItems 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:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}


// Configure the cell...
NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
cell.text = cellValue;



return cell;
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
    if(count < 4)
    {
    [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
    [selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];
        count++;
    }

} else {
    [selectedCell setAccessoryType:UITableViewCellAccessoryNone];
    [selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]];
    count --;
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];

}


- (void)dealloc {

[listOfItems release];
[super dealloc];
}


@end
4

6 回答 6

4

最简单的方法是 make 方法,它将返回您需要的字符串。方法看起来像这样:

- (NSString *) selectedItems {
    NSMutableString *result = [NSMutableString string];
    for (int i = 0; i < [itemsArray count]; i++) {
        NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
        [tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
        if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
            [result appendFormat:@"%@",cell.textLabel.text];
        }
    }
    if (result.length > 2) {
        [result replaceCharactersInRange:NSMakeRange(result.length-1, 1) withString:@""];
    }
return result;
}

要在另一个视图控制器中获取此行,您应该在 navigationController.viewController 中找到心理视图控制器并调用此方法。

- (void) method {
    for (UIViewController *vc in self.navigationController.viewControllers) {
        if ([vc isKindOfClass:[Psychological class]]) {
            NSString *str = vc.selectedItems;
        }
    }
}
于 2012-07-17T11:56:23.470 回答
1

希望以下代码符合您的要求:

//didSelect 方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath         *)indexPath {

    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

    if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {

     if(count < 4) {

         [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
         [selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];
         count++;
     }

  } else {

    [selectedCell setAccessoryType:UITableViewCellAccessoryNone];
    [selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]];
    count --;
}

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

全局声明 selectedCell 以在所有选项卡中访问

NSMutableString *selectedCell = [[NSMutableString alloc]init]; //Should declare this string globally

//在 -viewWillDisappear 方法或您认为合适的地方使用以下代码(从当前类导航时调用的方法)

for(int i = 0; i < [selectedIndexes count]; i++){

 [selectedCell appendString:[selectedIndexes objectAtIndex:i];
 [selectedCell appendString:@","];

}

NSRange range = {[selectedCell length]-1,1};
[selectedCell deleteCharactersInRange:range];
于 2012-07-17T11:56:18.177 回答
1
NSMutableString *selectedRow=[NSMutableString alloc]init];

for(int i=0; i<[count];i++)
{
    selectedRow=[arrayName objectAtIndex:row];
    [selectedRow appendString:@","];
}
于 2012-07-17T14:37:37.820 回答
1
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

    if (selectedCell.accessoryType == UITableViewCellAccessoryNone) {
if(count<4){
        selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
        [[listOfItems objectAtIndex:indexPath.row]setObject:@"YES" forKey:@"Selected"];
count++;
}

    } else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark) {
        if(count>=0){
        selectedCell.accessoryType = UITableViewCellAccessoryNone;
        [[listOfItems objectAtIndex:indexPath.row]setObject:@"NO" forKey:@"Selected"];
count--;
    }  
} 

替换上面的代码,当你想在那个时候浏览你的页面时检查 Selected 值是或否,如果是,则传递该值,否则离开它。

for (int i =0 ; i<[listOfItems count]; i++) {
        if ([[[listOfItems objectAtIndex:i]valueForKey:@"Selected"]isEqualToString:@"YES"]) {
            if ([selectedId length]==0) {
                selectedId = [NSString stringWithFormat:@"%@",[[listOfItems objectAtIndex:i]valueForKey:@"user_id"]];
            }else {
                selectedId = [selectedId stringByAppendingFormat:@",%@",[[listOfItems objectAtIndex:i]valueForKey:@"user_id"]];
            }
        }
    }
于 2012-08-08T06:30:32.163 回答
0

您可能想要一个全局数组。将选定的值存储到该全局数组中,然后很容易在其他地方显示它们.. :-)

于 2012-07-17T11:50:06.510 回答
0

创建一个新类,我们将其命名为Option,并为其赋予两个属性。一个是 NSString 调用name,另一个是 BOOL 调用selected。与其listOfItems作为字符串数组,不如将其设为Option对象列表。让每个Option人跟踪它当前是否被选中。

将拥有的控制器类listOfItems设为 aUITabBarControllerDelegate并实现tabBarController:didSelectViewController:,以便它构建字符串并将其传递给下一个选定的控制器。

于 2012-07-17T11:51:09.943 回答