【发布时间】: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