我从包含乘客详细信息的服务器解析数据。 乘客的详细信息保存在乘客舱。 主viewController包含一个UITableView,它可以加载乘客数据并允许用户重新排列单元格顺序。
我的问题是如何保存表格新订单并在每次应用程序启动时再次加载它,但是从服务器解析了新的乘客详细信息。 我不喜欢使用核心数据。
以下是代码:
#import <Foundation/Foundation.h>
@interface Passenger : NSObject
{
NSString *name;
NSString *code;
NSString *country;
NSString *date;
}
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *code;
@property (nonatomic, strong) NSString *country;
@property (nonatomic, strong) NSString *date;
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSString *stringToMove = passengersArray[sourceIndexPath.row];
[passengersArray removeObjectAtIndex:sourceIndexPath.row];
[passengersArray insertObject:stringToMove atIndex:destinationIndexPath.row];
}
- (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];
}
Passenger *current = [passengersArray objectAtIndex:indexPath.row];
UILabel *nameLabel = (UILabel *)[cell viewWithTag:101];
nameLabel.text = current.name;
UILabel *codeLabel = (UILabel *)[cell viewWithTag:102];
codeLabel.text = current.code;
UILabel *countryLabel = (UILabel *)[cell viewWithTag:103];
countryLabel.text = current.country;
return cell;
}
答案 0 :(得分:0)
您可以以数组的形式从plist加载乘客列表。当您更新迭代该plist数组的信息时,请比较,更新并将新乘客添加到列表中。用户重新排列订单后,您将其保存到plist。
NSPropertyListFormat format;
NSString *errorDesc;
NSString * plistPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
plistPath = [plistPath stringByAppendingPathComponent:@"Passengers.plist"];
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSArray *temp = (NSArray *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&errorDesc];
NSString *error;
NSString *plistPath;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
plistPath = [rootPath stringByAppendingPathComponent:@"Passengers.plist"];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:_passengersArray format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(plistData)
{
[plistData writeToFile:plistPath atomically:YES];
}
else
{
NSLog(@"Error writing passengers to file %@", error);
}