UItableView referencing sections from core data:
I am saving some data the user inputs into a data file using SQLITE, I am saving date, time, reading, and a note, as the following:
//Create a Reading Object
Readings *readingObj = [[Readings alloc] initWithPrimaryKey:0];
readingObj.date = date.text;
readingObj.time = time.text;
readingObj.reading = reading.text;
readingObj.note = note.text;
//Add the object //This is where I add the inputs to the SQL data file
[appDelegate addReading:readingObj];
My problem or question is: I want to sort the data in a uitableview with date as the section header and other inputs under the section in a cell. The dates represent the number of sections I would have.
This is what I have for now:
- (NSInteger)numberOfSectionsInTableView:(UITableVie w * )tableView {
return 1;
}
- (NSInteger)tableView:(UITableView * )tableView numberOfRowsInSection:(NSInteger)section {
return [appDelegate.readingsArray count];
}
- (UITableViewCell * )tableView:(UITableView * )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
HistoryCell *cell = (HistoryCell * )[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[His开发者_如何学CtoryCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
//Get the object from the array.
Readings *readingObj = [appDelegate.readingsArray objectAtIndex:indexPath.row];
[cell setTodo:readingObj];
return cell;
}
readingsArray is the array where I load all the data to; it has all the data:
NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
Readings *readingObj = [[Readings alloc] initWithPrimaryKey:primaryKey];
readingObj.date = [NSString stringWithUTF8String:(char * )sqlite3_column_text(selectstmt, 1)];
////change to int
readingObj.time = [NSString stringWithUTF8String:(char * )sqlite3_column_text(selectstmt, 2)];
////
readingObj.reading = [NSString stringWithUTF8String:(char * )sqlite3_column_text(selectstmt, 3)];
////
readingObj.note = [NSString stringWithUTF8String:(char * )sqlite3_column_text(selectstmt, 4)];
[appDelegate.readingsArray addObject:readingObj];
[readingObj release];
How can I have multiple sections based on the dates, when I only have one array, which readingsArray that has all the data. couldn't make a dates array because it was confusing for me. The dates sections would have multiple readings during that day.
Any suggestions or thoughts? I can provide more code or explanation about my code if needed.
In the title you mentioned once core data. So you probably have a NSFetchedResultsController in your delegate that fetches all the appropriate objects from core data! Then you should set a NSFetchedResultsController property in your class and set it to the delegates fetchedresultscontroller, so you have can use the fetchedresultscontroller delegate methods very comfortable. Or you just initialize another fetchedresultscontroller. Why do you set the fetchedresultsc. not directly in your tableviewcontroller class?
精彩评论