【问题标题】:iPhone: Repeating Rows in Each Section of Grouped UITableviewiPhone:在分组 UITableview 的每个部分中重复行
【发布时间】:2010-03-25 20:04:18
【问题描述】:

我正在尝试学习如何将 UITableView 与 SQLite 后端结合使用。我的问题是我已经得到了表格来填充数据库中的记录,但是我遇到了部分标题的问题。我无法为此找出正确的设置,并且我正在重复每个部分下的所有任务。

桌子看起来像这样。组字段是我试图从中提取部分标题的地方。

TaskID  groups      TaskName    sched   lastCompleted   nextCompleted   success
1       Household   laundry     3       03/19/2010      03/22/2010      y 
1       Automotive  Change oil  3       03/20/2010      03/23/2010      y 

在我的 viewDidLoad 方法中,我从表中的每一列创建一个数组,如下所示。

    //Create and initialize arrays from table columns
    //______________________________________________________________________________________

    ids =[[NSMutableArray alloc] init];
    tasks =[[NSMutableArray alloc] init];
    sched =[[NSMutableArray alloc] init];
    lastComplete =[[NSMutableArray alloc] init];
    nextComplete =[[NSMutableArray alloc] init];
    weight =[[NSMutableArray alloc] init];
    success =[[NSMutableArray alloc] init];
    group =[[NSMutableArray alloc] init];



// Bind them to the data
//______________________________________________________________________________________    
    NSString *query = [NSString stringWithFormat:@"SELECT * FROM Tasks ORDER BY nextComplete "];

    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2( database, [query UTF8String],
                           -1, &statement, nil) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            [ids addObject:[NSString stringWithFormat:@"%i",(int*) sqlite3_column_int(statement, 0)]];
            [group addObject:[NSString stringWithFormat:@"%s",(char*) sqlite3_column_text(statement, 1)]];
            [tasks addObject:[NSString stringWithFormat:@"%s",(char*) sqlite3_column_text(statement, 2)]];
            [sched addObject:[NSString stringWithFormat:@"%i",(int*) sqlite3_column_int(statement, 3)]];
            [lastComplete addObject:[NSString stringWithFormat:@"%s",(char*) sqlite3_column_text(statement, 4)]];
            [nextComplete addObject:[NSString stringWithFormat:@"%s",(char*) sqlite3_column_text(statement, 5)]];
            [success addObject:[NSString stringWithFormat:@"%s",(char*) sqlite3_column_text(statement, 6)]];
            [weight addObject:[NSString stringWithFormat:@"%i",(int*) sqlite3_column_int(statement, 7)]];




        }
        sqlite3_finalize(statement);
    }

在表格方法:cellForRowAtIndexPath 中,我动态创建控件并将它们的文本属性设置为数组中的对象。下面是一个示例,我可以提供更多,但我已经在这里写了一本书...... :)

/create the task label 
NSString *tmpMessage;
tmpMessage = [NSString stringWithFormat:@"%@ every %@ days, for %@ points",[tasks objectAtIndex:indexPath.row],[sched objectAtIndex:indexPath.row],[weight objectAtIndex:indexPath.row]];
    CGRect schedLabelRect = CGRectMake(0, 0, 250, 15);
    UILabel *lblSched = [[UILabel alloc] initWithFrame:schedLabelRect];
    lblSched.textAlignment = UITextAlignmentLeft;
    lblSched.text = tmpMessage;
    lblSched.font = [UIFont boldSystemFontOfSize:10];
    [cell.contentView addSubview: lblSched];
    [lblSched release];

我的 numberOfSectionsInTableView 方法看起来像这样

// Figure out how many sections there are by a distinct count of the groups field
// The groups are entered by user when creating tasks   
//______________________________________________________________________________________    
    NSString *groupquery = [NSString stringWithFormat:@"SELECT COUNT(DISTINCT groups) as Sum FROM Tasks"];
    int sum;

    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2( database, [groupquery UTF8String],
                           -1, &statement, nil) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            sum = sqlite3_column_int(statement, 0);

        }
        sqlite3_finalize(statement);
    }
    if (sum=0) {
        return 1;
    }
    return 2;
}

我知道我在这里出错了,但这就是我的 numberOfRowsInSection 方法中的全部内容

return [ids count];

【问题讨论】:

    标签: sqlite uitableview iphone-sdk-3.0


    【解决方案1】:

    从数据模型的角度来看,这就是您希望表格的样子:

    "Household" section
        record 1 of (all records whose "group" column=="Household")
        record 2 of (all records whose "group" column=="Household")
        record 3 of (all records whose "group" column=="Household")
    "Automotive" section
        record 1 of (all records whose "group" column=="Automotive")
        record 2 of (all records whose "group" column=="Automotive")
        record 3 of (all records whose "group" column=="Automotive")
    

    因此,您的数据模型必须反映这种结构。现在,它没有。您只需将每一列加载到一个数组中,但数组之间没有关系,例如“组”数组中的每个元素与任何其他数组中的任何元素都没有关系。这意味着您无法将每个“组”与同一记录中的其他列相关联。

    由于 SQL 没有结构,你必须提供一个对象层来将非结构化的 SQL 返回转换为结构化的。

    (我的 SQL 生锈了,所以下面的所有 SQL 都是伪代码。)

    // for simplicity, assume that the SQL table is indexed and always returns the same order
    @property (nonAtomic,retain) NSArray *group;
    @sythesize group;
    
    - (void) viewWillAppear:(BOOL)animated{
        // here groups is the column with "Household", "Automotive" etc
        // You want them unique because you want all "Household" under the same section
        NSArray *unSortedgroup=SQL(every unique name in column "group");
        self.group=[unsortedSectionName arraySortedUsing...]; //Sort the array however you wish
    }//------------------------------------viewWillAppear:------------------------------------
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return [self.group count];
    }
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
        return [self.group objectAtIndex:section];  
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        NSString *sectionName=[self.group objectAtIndex:section];
        NSUInteger *rowCount=SQL(count of all records whose "group" column == sectionName);
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        //... setup cell
    
    
        NSString *sectionName=[self.group objectAtIndex:section];
        NSArray *rowsForThisSection=SQL(indexs of all records whose "group" column == sectionName);
        // the data for each row cell is now in/pointed to by [rowsForThisSection objectAtIndex:indexPath.row]
        cell.idLabel.text=SQL(column "id" of record whose index is [rowsForThisSection objectAtIndex:indexPath.row])
    
        // ... and so on for your other columns/attributes      
    }
    

    这就是创建 Core Data 的原因。 SQL 和其他串行形式必须硬塞到面向对象 API 使用的对象图中,这需要大量的胶水代码。使用 Core Data,您可以创建一个对象图,然后让后端担心它是如何存储到磁盘上的。

    使用 Core Data,您可以用大约 8 行代码加上图形数据模型来完成所有这些工作。

    【讨论】:

    • 你真是技术禅!我不敢相信在完成了所有这些工作之后,我仍然做出了令人头疼的举动并混合了 HTML/标记。我最初确实走核心数据路线,但发现尝试同时学习 UITableView 和核心数据远远超出了我的豌豆大小的大脑所能处理的范围! SQl 伪代码不是问题。我这样做是为了一天的工作。再次感谢您!
    • 如果合适,我们将不胜感激。这也让其他人在未来更愿意回答你的问题。
    • 我快到了,现在正在尝试实施解决方案。可能并不重要,但所有这些数组之间的关系是它们的选择顺序。因此,如果我从同一个 select 语句中将这些数组填充为列,理论上,在我看来,它们的索引位置将决定它们的行。因此,上表中记录 2 的所有值将在每个数组中保存 objectAtIndex 2 位置,并且每次应用运行或 viewDidLoad 时始终保存该位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    相关资源
    最近更新 更多