【问题标题】:Populating sections in a UITableview using an array of dictionaries使用字典数组填充 UITableview 中的部分
【发布时间】:2013-01-27 03:23:47
【问题描述】:

我对 iOS 很陌生,但我已经到了那里,但遇到了一个问题,我很确定我只是不知道要使用的语法。

如何使用字典数组填充 UITableview,并使用字典中的特定项目填充两个部分?例如,我从一个网站读取了一个 plist 文件,它是一个字典数组。在字典项目中是一个称为“类型”的标识符。有两个不同的“类型”变量。一个表示“链接”,一个表示“显示”。我想要做的是在一个部分中有一个“链接”和另一部分中的“显示”的分段表。

这是我的代码,它将读取整个数组并将其放入一个部分,但我怎样才能让它分为两个部分?

这是 plist 的示例。注意“类型”,一个是“weblink”,一个是“show”。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>Title</key>
        <string>Faceboook</string>
        <key>Type</key>
        <string>weblink</string>
        <key>MapAddress</key>
        <string></string>
        <key>Address</key>
        <string></string>
        <key>Date</key>
        <string></string>
        <key>Link</key>
        <string>http://www.facebook.com/myfacebook</string>
    </dict>
    <dict>
        <key>Title</key>
        <string>Bernardo Winery</string>
        <key>Type</key>
        <string>show</string>
        <key>MapAddress</key>
        <string>321+Lombard+St,+San+Francisco,+CA</string>
        <key>Address</key>
        <string>321 Lombard St, San Francisco, CA</string>
        <key>Date</key>
        <string>Nov 2nd and 3rd.</string>
        <key>Link</key>
        <string></string>
    </dict>
</array>
</plist>

这里是它将 plist 读入 NSMutableArray TableData 的地方。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if(self)
{
    NSError *error = nil;
    
    NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.mywebsite.com/ShowList.plist"]
                                                   encoding:NSUTF8StringEncoding
                                                      error:&error];
    URLString = nil;
    
    if(error == nil)
    {
        TableData = [[NSMutableArray alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://www.mywebsite.com/ShowList.plist"]];
    }
    else
    {
        NSString *errorString = @"No internet connection, these links may not work!";
        
        UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error"
                                                     message:errorString
                                                    delegate:nil
                                           cancelButtonTitle:@"OK"
                                           otherButtonTitles: nil];
        [av show];

        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"ShowList" ofType: @"plist"];
        TableData = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
    }
}
return self;
}

这里是 TableData 被输入到 tableView 的地方。

-(UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"cell"];

//    NSLog(@"The count: %i", [TableData count]);
//    NSLog(@"Terms array %@",TableData);
//    NSLog(@"value at index %i is %@", 1, [TableData objectAtIndex:1]);

NSDictionary *tmpDic = [[NSDictionary alloc] initWithDictionary:[TableData objectAtIndex:[indexPath row]] copyItems:YES];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
}

[[cell textLabel] setText:[tmpDic objectForKey:@"Title"]];

return cell;

}

提前感谢您的帮助。 温德尔

编辑: 我实现了以下-

if ([[tmpDic objectForKey:@"Type"] isEqualToString:@"weblink"]) {
    if(indexPath.section==0)
    {
        [[cell textLabel] setText:[tmpDic objectForKey:@"Title"]];
    }
}

if ([[tmpDic objectForKey:@"Type"] isEqualToString:@"show"]) {
    if(indexPath.section==1)
    {
        [[cell textLabel] setText:[tmpDic objectForKey:@"Title"]];
    }
}

但以下情况正在发生:

【问题讨论】:

  • 你想要什么??解释排序:)
  • 我想要两个部分,一个用于“weblink”,一个用于“show”。伊万的下面几乎让我在我需要的地方,但它把所有行都放在了两个部分。

标签: ios uitableview nsarray nsdictionary


【解决方案1】:

我会将您拥有的数据转换为数组数组,然后以通常的方式为分区表填充表格:

@implementation TableController {
    NSMutableArray *tableData;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"ShowList" ofType: @"plist"];
    NSArray *array = [[NSArray alloc]initWithContentsOfFile:plistPath];
    NSMutableArray *showArray = [NSMutableArray array];
    NSMutableArray *webArray = [NSMutableArray array];
    for (NSDictionary *dict in array) {
        if ([dict[@"Type"] isEqualToString:@"weblink"]) {
            [webArray addObject:dict];
        }else if ([dict[@"Type"] isEqualToString:@"show"]) {
            [showArray addObject:dict];
        }
    }
    tableData = [NSMutableArray arrayWithObjects:webArray,showArray nil];
    NSLog(@"%@",tableData);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return tableData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [tableData[section] count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"Web Links";
    }else if (section == 1) {
        return @"Show Locations";
    }
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.textLabel.text = tableData[indexPath.section][indexPath.row][@"Title"];
    return cell;
}

【讨论】:

  • 看起来很有希望,我一定会试一试的。
【解决方案2】:

你应该使用:

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"cell"];

NSDictionary *tmpDic = [[NSDictionary alloc] initWithDictionary:[TableData objectAtIndex:[indexPath row]] copyItems:YES];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
}
if(indexPath.section==0)
{
[[cell textLabel] setText:[tmpDic objectForKey:@"Title"]];
}else
{
[[cell textLabel] setText:[tmpDic objectForKey:@"Link"]];
}

return cell;

}

【讨论】:

  • 这可能是在正确的轨道上。 “标题”是填充单元格所需的内容,“weblink”在第 0 节中,“show”在第 1 节中。我尝试了以下内容,但它创建了两个完整的列表,每个部分中都有一个。 if(indexPath.section==0 && [[tmpDic objectForKey:@"Type"] isEqualToString:@"weblink"])
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多