我是新來的xcode和使我的第一個iPhone應用程序。如何將選定的行從UITableview保存到NSString變量?
我有一個標籤式屏幕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
嗨,這正是我正在尋找的。我只是沒有得到在哪裏調用這個方法? 這應該在下一個標籤中調用嗎?如果是這樣的話?對不起,這個愚蠢的問題,我真的很新的xcode! – 2012-07-17 12:09:07
是在下一個選項卡中。我認爲你需要在viewWillAppear:方法中調用它。 – Moonkid 2012-07-17 12:12:08
謝謝,在上面的代碼中,它給出了一個錯誤,說tableView未聲明? – 2012-07-17 13:18:12