【发布时间】:2017-08-03 11:02:07
【问题描述】:
我有一个任务,我必须在表格视图中加载以下项目
- 图片
- 标题
- 说明
页脚视图是一个简单的水平黑色规则,大约 5 磅。
但是只有图片是可见的,而不是标题和描述。不知何故,其他视图被图像视图遮挡了。
我的自定义表格视图单元实现
#import <UIKit/UIKit.h>
@interface MeowFestTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UILabel *descriptionLabel;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end
和实现
#import "MeowFestTableViewCell.h"
@implementation MeowFestTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
}
return self;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
我的视图控制器
#import "MeowFestViewController.h"
#import "MeowFestTableViewCell.h"
@interface MeowFestViewController ()
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray* feedItems;
@property (nonatomic, strong) NSMutableData* responseData;
@property (nonatomic, assign) CGPoint currentOffset;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation MeowFestViewController
-(void) awakeFromNib {
[super awakeFromNib];
}
-(void) loadView {
[super loadView];
// Force load.
UIView* view = self.view;
self.page = 0;
self.currentOffset = CGPointZero;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
self.meowFestProtocolDelegate = self;
[self.meowFestProtocolDelegate getTheFeed:self.page];
}
#pragma mark - MeowFestProtocol
-(void) getTheFeed:(NSInteger) page {
NSString* url =
[NSString stringWithFormat:@"https://chex-triplebyte.herokuapp.com/api/cats?page=%d", self.page];
NSURL* nsUrl = [[NSURL alloc] initWithString:url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nsUrl];
[request setHTTPMethod:@"GET"];
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
self.feedItems = [NSJSONSerialization JSONObjectWithData: data
options: NSJSONReadingMutableContainers error: &error];
[self.tableView reloadData];
dispatch_semaphore_signal(semaphore);
}];
[task resume];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
#pragma Table Arguments.
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.feedItems count] + 1; // (10 items + 1 button)
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
if (indexPath.row == 10) {
return 50.0f;
}
return 300.0f;
}
-(UIView*) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView* footerView =
[[UIView alloc] initWithFrame:CGRectMake(
0, 0, CGRectGetWidth(self.view.bounds), 5)];
[footerView setBackgroundColor:[UIColor blackColor]];
return footerView;
}
-(CGFloat)
tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 5.0f;
}
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row != 10) {
MeowFestTableViewCell* tableViewCell = (MeowFestTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"meowFestCell"];
if (tableViewCell == nil) {
tableViewCell = [[MeowFestTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"meowFestCell"];
}
NSDictionary* dict = (NSDictionary*) [self.feedItems objectAtIndex:(self.page * 10 + indexPath.row)];
// Initialise the data.
tableViewCell.titleLabel.text = (NSString*) [dict objectForKey:@"title"];
[tableViewCell.titleLabel sizeToFit];
tableViewCell.descriptionLabel.text = (NSString*) [dict objectForKey:@"description"];
[tableViewCell.descriptionLabel sizeToFit];
UIImage* catImage =
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[dict objectForKey:@"image_url"]]]];
tableViewCell.imageView.image = catImage;
return tableViewCell;
} else {
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"loadMoreCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"loadMoreCell"];
}
cell.backgroundColor = [UIColor colorWithRed:0.50 green:0.00 blue:0.50 alpha:1.0];
return cell;
}
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// rows in section 0 should not be selectable
if (indexPath.row == 10) {
return indexPath;
}
return nil;
}
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint offset = scrollView.contentOffset;
CGSize size = scrollView.contentSize;
if (abs(size.height - offset.y) < 10) {
self.currentOffset = offset;
} else if (offset.y < self.currentOffset.y) {
if (offset.y == 0) {
self.page = 0;
self.currentOffset = CGPointZero;
}
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == [self.feedItems count]) {
[self.meowFestProtocolDelegate getTheFeed:(self.page + 1)];
self.page = self.page + 1;
[self.tableView reloadData];
}
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
更新
我尝试按照@vadian 的建议添加缺少的约束,但该选项对我来说是禁用的。
更新#2:@vadian 的设置两个 UILabel 的高度约束有效,但是有没有办法不必硬编码 UILabel 的高度?我通过 API 调用获取内容,因此我无法控制返回的文本。文本可以尽可能长。我已经设置了行数=0,而换行模式=Word Break。
【问题讨论】:
-
给描述标签设置底部约束,你会得到错误然后通过xcode更正解决错误。
-
嗨,我已经为描述标签定义了约束。现在,superview.bottom = descriptonLabel.bottom + 35 和 descriptionlabel.top = titleLabel.bottom + 35
-
那么为什么它会显示约束警告。
-
使用 tableView 的 heightForRow_atIndexPath 委托方法。并返回您想要的任何行的高度,但请确保您必须正确地将约束添加到标签(顶部,前导尾随)是强制性的(您也可以在容器中水平使用宽度和中心而不是前导和尾随) .
-
使用垂直内容拥抱和内容压缩而不是高度限制。 (见我的回答。)
标签: ios objective-c uitableview user-interface