【问题标题】:reorder and save UITableView重新排序并保存 UITableView
【发布时间】:2014-09-10 13:57:02
【问题描述】:

我从包含乘客详细信息的服务器解析数据。 乘客详细信息保存到乘客类中。 主 viewController 包含一个 UITableView,它加载乘客数据并允许用户重新排列单元格的顺序。

我的问题是如何保存表格新订单并在每次应用程序启动时重新加载,但从服务器解析新乘客详细信息。 我不喜欢使用核心数据。

代码如下:

乘客.h

#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;

主视图控制器 m

- (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;
}

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    您可以从数组形式的 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);
    }
    

    【讨论】:

    • 谢谢,我的主要问题是比较和更新数据,但在我设法做到这一点后,我将订单保存在 nsuserdefaults 中,这对我有用。
    • 嗨,我如何在应用启动时加载重新排序的索引路径。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 2011-01-22
    • 1970-01-01
    相关资源
    最近更新 更多