从plist iOS获取某些字符串

时间:2015-01-19 20:50:10

标签: ios objective-c plist

我有一个plist,我只想抓住名字'字符串以' A'开头。我想在我的UITableView中显示这些内容,以及' NameOrigin'包含在该项目中。我已经包含了我的plist的屏幕截图。感谢

enter image description here

我的代码到目前为止,我能够显示所有plist项目,但我想将显示的项目过滤为带有字符串' Name'跟着这封信' A'?

-(NSArray *)content
{
    //if (!_content) {
    _content = [[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"MyPLIST" ofType:@"plist"]];
    // }
    return _content;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [TableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = [[self.content objectAtIndex:indexPath.row] objectForKey:@"Name"];
    cell.detailTextLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"NameOrigin"];

    return cell;
}

1 个答案:

答案 0 :(得分:0)

如果您只想过滤Name以“A”开头的元素,请尝试以下操作:

NSArray *filteredArray = [self.content filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"Name beginswith %@", @"A"]];

虽然我们正在努力:你应该考虑重构你的代码。每次调用[self content] 重新加载文件中的数据都是错误的想法,性能明智。相反,试试这个:

// Create a property called content in your class extension on the top of the .m file of your table view controller
@interface YourTableViewController ()
@property (nonatomic, strong) NSArray *content;
@end

接下来,在viewDidLoad回调中添加此代码:

_content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"MyPLIST" ofType:@"plist"]];

并删除自定义content方法。