如何在Table View中刷新plist中的数据?

时间:2014-06-20 09:35:15

标签: ios iphone objective-c cocoa-touch uitableview

所以我有这种情况。我有一个表视图,带有来自NIB的自定义单元格。我将数据从plist中提取到每个单元格的属性中。问题是,当我更改视图时,从该视图我改变数据应该来自数据,返回到表视图,数据不会刷新,应用程序将崩溃。 谢谢你的每一个建议!

我的.m文件如下所示:

#import "TableViewController.h"
#import "TableViewCell.h"

static NSString *CellTableIdentifier = @"TableViewCell";

@interface TableViewController ()

@property (strong, nonatomic) IBOutlet UIButton *citiesButton;

@property (copy, nonatomic) NSString *path;
@property (copy, nonatomic) NSDictionary *dict;
@property (copy, nonatomic) NSArray *city;
@property (copy, nonatomic) NSDictionary *rowData;

@end

@implementation TableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    rowValue = @"Brno";

    self.path = [[NSBundle mainBundle] pathForResource:@"Database2" ofType:@"plist"];
    self.dict = [[NSDictionary alloc] initWithContentsOfFile:_path]; //Main Dictionary
    self.city = [_dict objectForKey:rowValue];

    [self.citiesButton setTitle:rowValue forState:UIControlStateNormal];

UITableView *tableView = (id)[self.view viewWithTag:1];
{
    tableView.rowHeight = 100;
    UINib *nib  = [UINib nibWithNibName:@"MIKETableViewCell" bundle:nil];
    [tableView registerNib:nib forCellReuseIdentifier:CellTableIdentifier];

    UIEdgeInsets contentInset = tableView.contentInset;
    contentInset.top = 20;
    [tableView setContentInset:contentInset];

    NSLog(@"rowValue is %@", rowValue);
    self.city = [_dict objectForKey:rowValue];
}

    NSLog(@"Main view loaded successfuly");
}

-(void)viewWillAppear:(BOOL)animated
{
    [self.citiesButton setTitle:rowValue forState:UIControlStateNormal];
    NSLog(@"View will appear");



}

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.city count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    //this is space
    return 50;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier forIndexPath:indexPath];


    self.rowData = _city[indexPath.section];
    NSLog(@"All keys for dict: %@", [self.dict allKeys]);

    cell.timeLabel.text = _rowData[@"Time"];
    cell.priceLabel.text = _rowData[@"Price"];
    cell.infoLabel.text = _rowData[@"Info"];


    cell.infoLabel.transform = CGAffineTransformMakeRotation(3.14/2);

    return cell;
}

@end

0 个答案:

没有答案