将数据加载到tableview中,点击其他平板电脑中的行无效

时间:2014-08-12 14:53:44

标签: ios objective-c uitableview

我的视图控制器中有两个表视图,它们都由同一个控制器控制。 当用户单击第一个数据中的单元格时,应从数据库中取出并填充第二个数据。我有一切工作,实际上将数据加载到点击表中。 用户将点击的表格为_lst_CourseList,显示数据的表格为_tbl_notes

NSMutableArray *Notes;
- (void)viewDidLoad
{
    ...
    Notes = [[NSMutableArray alloc] init];
    ...
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == _lst_CourseList){
        return [courseSections count];
    }else{
        return 1;
    }
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    if (tableView == _lst_CourseList){
        if (alphabetic){
            return courseSections;
        }
        return nil;
    }
    return nil;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (tableView == _lst_CourseList){
        return [courseSections objectAtIndex:section];
    } else {
        return nil;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _lst_CourseList){
        NSString *sectionTitle = [courseSections objectAtIndex:section];
        NSArray *sectionCourses = [courseDict objectForKey:sectionTitle];
        return [sectionCourses count];
    } else {
        [Notes count];
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _lst_CourseList){
        ...
    } else {
        static NSString *cellIdentifier = @"notecell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        }
        // Configure the cell...
        KCNote *note = [Notes objectAtIndex:indexPath.row];

        UILabel *Author =(UILabel *) [cell viewWithTag:1];
        UILabel *Date = (UILabel *) [cell viewWithTag:2];
        UILabel *Note = (UILabel *) [cell viewWithTag:3];

        Author.text = note.Author;
        Date.text = note.Date;
        Note.text = note.Note;

        UIView *bgColorView = [[UIView alloc] init];
        bgColorView.backgroundColor = [UIColor colorWithRed:0.933 green:0.251 blue:0.208 alpha:1];
        bgColorView.layer.masksToBounds = YES;
        [cell setSelectedBackgroundView:bgColorView];
        return cell;
    }

}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
    if (tableView == _lst_CourseList){
        ...
        [self getNotes:course.id];
        ...
    }
}

-(void)getNotes:(NSInteger)courseid{
    [Notes removeAllObjects];
    FMResultSet *s;
    s = [db executeQuery:@"SELECT * FROM notes WHERE CourseID =?;", [NSString stringWithFormat:@"%d", courseid]];
    //s = [db executeQuery:@"SELECT * FROM notes;"];
    if ([db hadError]) {
        NSLog(@"DB Error %d: %@", [db lastErrorCode], [db lastErrorMessage]);
    }
    while ([s next]) {
        NSInteger NoteID = [s intForColumnIndex:0];
        NSString *Note = [s stringForColumnIndex:2];
        NSString *Author = [s stringForColumnIndex:3];
        NSString *date = [s stringForColumnIndex:4];
        KCNote *note = [KCNote new];
        note.NoteID = NoteID;
        note.Note = note;
        note.Author = Author;
        note.Date = date;
        [Notes addObject:note];
    }
    [_tbl_notes reloadData];
}

我可以看到在数据库和getNotes末尾找到了Notes。数组包含了所有需要显示的注释对象,但似乎reloadData没有重新加载显示此数据

2 个答案:

答案 0 :(得分:0)

你是否将两个表的委托和dataSource设置为self?

答案 1 :(得分:0)

您忘记将返回置于 [NOTES count]之前; 见下文:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _lst_CourseList){
        NSString *sectionTitle = [courseSections objectAtIndex:section];
        NSArray *sectionCourses = [courseDict objectForKey:sectionTitle];
        return [sectionCourses count];
} else {
    return [Notes count];
}
return 0;
}