【问题标题】:UITableView with sections of category带有类别部分的 UITableView
【发布时间】:2013-11-13 13:02:26
【问题描述】:

有几个问题与此类似,但我就是不知道如何处理。

我正在解析一个类似于这个的xml;

标题-a 内容-b 类别-cat1

标题-c 内容-d 类别-cat2

等等..

然后我将捕获的所有值保存到 nsmutable 数组中。所以我有标题、内容和类别的 nsmutable 数组。例如,数组中该对象的所有值的索引为 10 的对象([title indexOfObject: 10],[内容 indexOfObject:10],[类别 indexOfObject:10])

解析后,我得到这个循环有多少类别;

mySections=[[NSMutableArray alloc] init];
    for (int i=0; i<[category count]; i++) {
        if (![mySections containsObject:[category objectAtIndex:i]]) {
            [mySections addObject:[category objectAtIndex:i]];
        }
    }
//--------------------


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return [mySections count];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger count=0;
    for (int i=0; i<[baslik count]; i++) {
        if ([[category objectAtIndex:i]isEqualToString:[category objectAtIndex:section]]) {
            count++;
        }
    }
    return count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

        return [mySections objectAtIndex:section];
}


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

//But in this function I cannot figure out how to access the right element 

[title objectAtIndex:indexPath.row]

}

我知道我可能做错了,但这是我第一次为部分编码。有人可以帮助我如何以正确的方式编写它吗?

【问题讨论】:

    标签: ios iphone objective-c uitableview sections


    【解决方案1】:

    一个好的方法是将所有数据填充到一个 NSDictionary 中,其格式类似于

    第 1 节 项目 第2节 项目

    那么你可以这样做:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
        return [yourDictionary count];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        NSString *aSection = yourDictionary.allKeys[section];
        id theData = yourDictionary[aSection];
        return [theData count];
    }
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
    
        return yourDictionary.allKeys[section];
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *aSection = yourDictionary.allKeys[indexPath.section];
        id theData = yourDictionary[aSection][indexPath.row];
        ....
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      • 2013-07-09
      • 2014-01-21
      • 2010-11-06
      • 2012-07-10
      相关资源
      最近更新 更多