0
我有父級和子級視圖控制器,在子視圖控制器中我有圖像視圖和表視圖。如果表格視圖的內容大小小於屏幕大小,則會正常進行。但是,如果內容大小超過屏幕大小,我無法滾動它。
在XCode屬性檢查器我有這些屬性。 http://i.stack.imgur.com/yuxZ1.png
我的代碼來生成表視圖內容:在PageViewController中滾動TableView
- (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如果表內容大小超過屏幕大小?
感謝您的回答!你能幫我解決另一個問題:如果我使用「建議約束」,在3.5英寸的圖像視圖減少,但我不想要它,我只需要設置約束到我的表視圖的底部。我必須做什麼?在我的語言中沒有很多自動佈局的材料... – SergeK
您可以在viewcontroller的xib中添加uitableview的標題,然後您可以設置imageview的高度約束,如果您要更改imageview的高度,它總是會以該尺寸顯示imageview比你可以創建一個nslayourconstraint的引用來在你的代碼中實際處理。 –