변수 테이블 셀 높이가있는 iOS7 및 iOS8에서 실행하려는 앱을 만들고 있습니다. iOS7에서는 프로토 타입 셀을 정의한 다음 heightForRowAtIndexPath에서이 값을 사용하여 레이블에 배치 된 텍스트의 크기를 기반으로 높이를 계산했습니다.iOS7 및 iOS8과 호환되는 동적 높이를 가진 UITableViewCell을 어떻게 만듭니 까?
이것은 Xcode 6을 사용할 때 더 이상 작동하지 않는 것처럼 보입니다. 오늘 미심쩍은 지 확인하기 위해 작은 테스트 응용 프로그램을 만들었습니다. 나는 heightForRowAtIndexPath를 전혀 정의하지 않음으로써 시작했다. iOS7 및 iOS8 장치로 테스트 할 때 동작이 다르게 작동합니다. iOS8 장치에서는 셀 높이를 자동으로 결정하여 정상적으로 작동합니다. iOS7에서는 셀이 모두 동일한 높이로 반환됩니다. iOS7에서는 변수 텍스트가있는 셀의 자동 높이 조정을 수행하지 않고 iOS8에서만 자동 높이 조정을 수행합니다.
그래서 나 ... 흠 ... 생각 ... 나는 iOS7에 대한 heightForRowAtIndexPath을 높이를 결정만을 구현하는 자동 레이아웃을 사용할 수 있습니다 iOS8의 아래 ...
좋아, 그래서 ... 내가 추가하는 시도 heightForRowAtIndexPath에 있습니다. iOS7에서는 systemLayoutSizeFittingSize 메소드에서 충돌합니다. 나는 몇 시간 동안 이걸 가지고 놀았 어. 매우 간단한 코드입니다. 여기
가있는 UITableViewController 코드 : 여기//
// Cell1.h
// testTables
//
#import <UIKit/UIKit.h>
@interface Cell1 : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *label1;
@end
가 셀의 XIB입니다 :
여기//
// TestTableViewController.m
// testTables
//
#import "TestTableViewController.h"
#import "Cell1.h"
@interface TestTableViewController()
@property (nonatomic, strong) Cell1 *cellPrototype;
@end
@implementation TestTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"Cell1" bundle:nil] forCellReuseIdentifier:@"Cell1"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
self.cellPrototype = [self.tableView dequeueReusableCellWithIdentifier:@"Cell1"];
self.cellPrototype.label1.text = [self cellContents:indexPath];
[self.cellPrototype layoutIfNeeded];
CGSize size = [self.cellPrototype systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Cell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell1" forIndexPath:indexPath];
cell.label1.text = [self cellContents:indexPath];
return cell;
}
- (NSString*)cellContents:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
return @"one line";
}
else if (indexPath.row == 1) {
return @"two lines I think will result from this piece of text that goes over";
}
else if (indexPath.row == 2) {
return @"three lines I think will result from this piece of text that goes over and then even extends a little more again";
}
else {
return @"three lines I think will result from this piece of text that goes over and then even extends a little more again, and then extends over more and more lines like it will go on forever and never end but in fact it does end right here.";
}
}
@end
셀 헤더입니다합니다 (하는 .m 파일에 아무것도 없다). 보시다시피 복잡하지 않습니다.
...이 오래된 방법이 더 이상 작동하지 않습니까? 어떻게 iOSes에서 실행될 가변 높이 셀을 얻을 수 있습니까?
몇 가지 문제 만 제외하면 제대로 된 것처럼 보입니다. 셀의 컨텐트 뷰에서 systemLayoutSizeFittingSize를 호출하면 안됩니까? 프로토 타입을 한 번 만들어서 테이블에서 큐를 뽑아내는 대신 재사용하면 안됩니까? – CrimsonChris
@CrimsonChris 감사합니다. 예, 나는 그 두 가지를 시도했지만 아무런 차이가 없었습니다. contentView에서 systemLayoutSizeFittingSize를 시도하고 프로토 타입을 인스턴스화하는 여러 가지 방법을 시도했으며 모두 동일한 결과를 보였습니다. 모두 제약 조건에 대해 불평하는 systemLayoutSizeFittingSize에서 실패합니다. – Darren
대상을 iOS7로 설정하면 인터페이스 작성기가 경고를 표시합니다. 자동 레이아웃 제약 조건이 인터페이스 빌더에서 Xcode 6으로 변경되었습니다. 이전 버전과 호환되지 않는 제약 조건을 사용하는 경우 경고가 표시됩니다. – CrimsonChris