如何确定UITableView中的单元格部分 - objective-c



im 在具有搜索视图的应用程序上工作,搜索视图包含一个包含 3 个部分的表视图 - 我通过以下方式手动实现该部分

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    // Return the number of sections.
    return 3;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0){
        UITextField *title = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 320, 10)];
        [title setBackgroundColor:[UIColor lightGrayColor]];
        [title setTextAlignment:UITextAlignmentRight];
        [title setFont:[UIFont boldSystemFontOfSize:15]];
        [title setText:@"videos"];
        return title ;
    }
    if (section == 1){
        UITextField *title = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 320, 10)];
        [title setBackgroundColor:[UIColor lightGrayColor]];
        [title setTextAlignment:UITextAlignmentRight];
        [title setFont:[UIFont boldSystemFontOfSize:15]];
        [title setText:@"documents"];
        return title ; 
    }
    if (section == 2){
        UITextField *title = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 320, 10)];
        [title setBackgroundColor:[UIColor lightGrayColor]];
        [title setTextAlignment:UITextAlignmentRight];
        [title setFont:[UIFont boldSystemFontOfSize:15]];
        [title setText:@"questions"];
        return title ; 
    }
}

现在,我用XML接收的所有数据,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"> 
<channel>
    <title>documents</title>
        <item>
        <title>how to count the days</title>
        <link>some link...</link>
        <date>11/09/2007 16:45:00</date>
        <SupplierName>roei</SupplierName>
        <CategoryName>the omer</CategoryName>
      </item>
        <item>
        <title>research</title>
        <link>some link...</link>
        <date>16/05/2010 13:41:00</date>
        <SupplierName>eran</SupplierName>
        <CategoryName>shavoot</CategoryName>
      </item>
</channel>
<channel>
    <title> video </title>
                <item>
                <title>the end of holiday</title>
                <link>some link...</link>
                <date>06/06/2011 08:56:00</date>
                <SupplierName>dina</SupplierName>
                <CategoryName>shavoot</CategoryName>
              </item>
                <item>
                <title>bible</title>
                <link>some link...</link>
                <date>24/05/2012 10:00:00</date>
                <SupplierName>amir</SupplierName>
                <CategoryName>shavoot</CategoryName>        
              </item>
</channel>

        <channel>
            <title>questions</title>
                <item>
                <title>questions for shavvot</title>
                <link>some link...</link>
                <date>28/10/2008 13:31:00</date>
                <SenderName>אלי.ד.</SenderName>
                <CategoryName>holidays</CategoryName>
              </item>
                <item>
                <title>Shavoot</title>
                <link>some link...</link>
                <date>25/05/2008 01:01:00</date>
                <SenderName>amir</SenderName>
                <CategoryName>holidays</CategoryName>
              </item>

        </channel>

</rss>

如您所见-我有带有部分名称的标题,但是我应该如何确定单元格的正确部分?我将所有数据解析为一个数组,然后使用字典将其取出:

NSDictionary *object = [ResultsArrayDisplay objectAtIndex:indexPath.row];

请帮助我,经过几个小时的尝试,到目前为止,互联网上没有任何帮助。

这是你的问题:

IM 将所有数据解析到一个数组,然后使用 字典:

将每个部分的数据放在其自己的数组中,然后使用 indexPath.section 来确定要使用的数组。 对于每个部分,它只会为您提供该部分的行,以便您可以使用indexPath.row索引到每个数组中。

如果您坚持将所有内容保存在一个数组中,那么您将不得不维护每种类型的数量,然后使用它来确定您需要哪个数组索引。

无论哪种情况,请务必实现numberOfRowsinSection: 。 如果您使用多个数组,那么这只是该部分的数组计数,否则它将是该部分中使用的主数组部分。