在PageViewController中滚动TableView

时间:2014-04-15 11:43:11

标签: ios objective-c uitableview uiscrollview uipageviewcontroller

我有父视图控制器和子视图控制器,在子视图控制器中我有图像视图和表视图。如果表格视图的内容大小小于屏幕大小,则表明其正常运行。但如果内容大小超过屏幕大小,我无法滚动它 在XCode Attributes Inspector中,我有这些属性。 http://i.stack.imgur.com/yuxZ1.png
我的代码生成表格视图内容:

- (void)viewDidLoad {
[super viewDidLoad];
_mainArray = [self getAnswers];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSUInteger)section {
int height =  (_imageView.image == 0) ? 0 : 118;
return height;
}

- (NSMutableArray *)getAnswers {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"pdd.sqlite"];
const char * dbpath = [defaultDBPath UTF8String];
sqlite3_stmt *statement;
NSMutableArray *array = [[NSMutableArray alloc] init];
if (sqlite3_open(dbpath, &_pdd_ab) == SQLITE_OK) {
    NSString *querySQL = [NSString stringWithFormat:@"SELECT RecNo, Picture, Question, Answer1, Answer2, Answer3, Answer4, Answer5, RightAnswer, Comment FROM paper_ab WHERE PaperNumber = \"%@\" AND QuestionInPaper = \"%d\"", _randomNumbers[_index] , (int)_index + 1];
    const char * query_stmt = [querySQL UTF8String];
    if (sqlite3_prepare_v2(_pdd_ab, query_stmt, -1, &statement, NULL) == SQLITE_OK) {
        if (sqlite3_step(statement) == SQLITE_ROW) {
            NSData *picture = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 1) length: sqlite3_column_bytes(statement, 1)];
            _imageView.image = [UIImage imageWithData:picture];
            for (int i = 2; i < 10; i++) {
                if (sqlite3_column_text(statement, i) != NULL) {
                    NSString *arrayelement = [[NSString alloc]
                                              initWithUTF8String:
                                              (const char *) sqlite3_column_text(statement, i)];
                    [array addObject:arrayelement];
                }
            }
            sqlite3_finalize(statement);
        } else {
            NSLog(@"Rezultatov net!");
        }
    } else {
        NSLog(@"Ne mogu vypolnit' zapros!");
    }
    sqlite3_close(_pdd_ab);
} else {
    NSLog(@"Ne mogu ustanovit' soedinenie!");
}
return array;
}

- (NSUInteger)numberOfSectionInTableView:(UITableView *)tableView {
return 1;
}

- (NSUInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSUInteger)section {
return _mainArray.count - 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger rowNumber = [indexPath row];
static NSString * CellIdentifier = @"Cell";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if (rowNumber == 0) {
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont italicSystemFontOfSize:15.0];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.text = [NSString stringWithFormat:@"%@", _mainArray[0]];
    cell.textLabel.numberOfLines = 0;
} else {
    cell.textLabel.font = [UIFont systemFontOfSize:15.0];
    cell.textLabel.text = [NSString stringWithFormat:@"%ld. %@", (long)rowNumber, _mainArray[rowNumber]];
    cell.textLabel.numberOfLines = 0;
}
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                      cell.textLabel.font, NSFontAttributeName,
                                      nil];
CGRect textLabelSize = [cell.textLabel.text boundingRectWithSize:kExamenLabelFrameMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDictionary context:nil];
cell.textLabel.frame = CGRectMake(5, 5, textLabelSize.size.width, textLabelSize.size.height);
return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger rowNumber = [indexPath row];
if ([self.rightAnswersArray containsObject:[NSNumber numberWithLong:_index + 1]] ) { // делаю красиво, если пользователь возвращается к вопросу, на который уже правильно ответил
    self.tableView.allowsSelection = NO;
    if (rowNumber == [_mainArray[_mainArray.count - 2] intValue]) { // если ответ правильный
        cell.contentView.backgroundColor = [UIColor colorWithRed:0 / 255.0f green:152 / 255.0f blue:70 / 255.0f alpha:1.0f];
    }
} else if ([self.wrongAnswersArray containsObject:[NSNumber numberWithLong:_index + 1]] ) { // делаю красиво, если пользователь возвращается к вопросу, на который уже НЕправильно ответил
    long questnum = [self.wrongAnswersArray indexOfObject:[NSNumber numberWithInteger:_index + 1]];
    self.tableView.allowsSelection = NO;
    if (rowNumber != [_mainArray[_mainArray.count - 2] intValue] && [NSNumber numberWithLong:rowNumber] == [NSNumber numberWithInt:[[self.wrongAnswersSelectedArray objectAtIndex:questnum] intValue]]) {  // если ответ НЕправильный
        cell.contentView.backgroundColor = [UIColor colorWithRed:236 / 255.0f green:30 / 255.0f blue:36 / 255.0f alpha:1.0f];
    }
}
}  
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSUInteger rowNumber = [indexPath row];
if (rowNumber == 0) {
    cell.contentView.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
} else {
    if (rowNumber == [_mainArray[_mainArray.count - 2] intValue]) { // если ответ правильный
        if ([settings boolForKey:@"needVibro"]) {
            AudioServicesPlayAlertSound(kSystemSoundID_Vibrate); // вибрация при правильном ответе
        }
        cell.contentView.backgroundColor = [UIColor colorWithRed:0 / 255.0f green:152 / 255.0f blue:70 / 255.0f alpha:1.0f];
        self.tableView.allowsSelection = NO;
        [self.rightAnswersArray addObject:[NSNumber numberWithLong:_index + 1]];
    } else { // если ответ неправильный
        cell.contentView.backgroundColor = [UIColor colorWithRed:236 / 255.0f green:30 / 255.0f blue:36 / 255.0f alpha:1.0f];
        self.tableView.allowsSelection = NO;
        [self.wrongAnswersArray addObject:[NSNumber numberWithLong:_index + 1]];
        [self.wrongAnswersSelectedArray addObject:[NSNumber numberWithLong:rowNumber]];
    }
}
NSUInteger wrongCount = _wrongAnswersArray.count;
NSUInteger rightCount = _rightAnswersArray.count;
NSLog(@"Номер вопроса - %d, правильных ответов - %d, неправильных ответов - %d", (int)_index + 1, (int)rightCount, (int)wrongCount);
if (rightCount + wrongCount == 20) {
    [self writeStatisticsToBase];
    [self getResultOfTest];
    [_timer invalidate];
}
}

- (void)getResultOfTest {
[self performSegueWithIdentifier:@"ResultExamen" sender:self];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger rowNumber = [indexPath row];
NSString *textLabel = [NSString stringWithFormat:@"%ld. %@", (long)rowNumber, _mainArray[rowNumber]];
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                      [UIFont systemFontOfSize:15.0f], NSFontAttributeName,
                                      nil];
CGRect textLabelSize = [textLabel boundingRectWithSize:kExamenLabelFrameMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDictionary context:nil];
return kExamenDifference + textLabelSize.size.height;
}  

如果我将“滚动视图”的“弹跳”和“弹跳”属性更改为启用,我可以滚动表视图,但它会返回其原始状态。您可以在YouTube http://www.youtube.com/watch?v=5rDd4oQQDB0

上看到这种情况

所以,我的问题是如果表内容大小超过屏幕大小,如何在UIPageViewController中滚动UITableView?

1 个答案:

答案 0 :(得分:0)

您必须根据设备屏幕尺寸将底部空间设置为超级视图自动布局到您的桌面视图管理滚动。底部空间到超级视图自动布局根据设备屏幕尺寸调整您的桌面视图底部。