2017-08-03 55 views
0

我在我必须加载在表视图以下项目的分配无法查看自定义标签中的UITableViewCell

  1. 图片
  2. 标题
  3. 说明

的页脚视图是一个简单的水平黑色约5分的规则。

但只有图像可见,但不是标题和描述。不知何故,其他视图被图像视图遮挡。

My Custom TableView Cell in Storyboard

我定制的tableview单元实现

#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 

在模拟器的输出如下Simulator Output and Analysis of Views

UPDATE

给我尝试添加缺少约束每@ vadian的建议,但是这个选项被禁用我。

Add Constraints disabledAll subviews selected

更新#2:@ vadian的设置既UILabels作品的高度限制,但有没有办法不用硬编码UILabels的高度?我通过创建API调用来获取内容,所以我无法控制返回的文本。文本可以尽可能长。我已经设置了行数= 0,而换行符模式= Word Break。

+0

给底部约束描述标签,你会得到错误然后通过Xcode的校正解决错误。 – KKRocks

+0

嗨,我已经定义了描述标签的约束。现在,superview.bottom = descriptonLabel.bottom + 35和descriptionlabel.top = titleLabel.bottom + 35 – Kartik

+0

那么为什么会显示约束警告。 – KKRocks

回答

1

要进行设置,您需要将垂直间距约束添加到标签。

  1. 在图像和顶部标签之间添加垂直间距约束。
  2. 在两个标签之间添加垂直间距约束。
  3. 在底部标签和包含视图的底部之间添加垂直间距约束。
  4. 现在,在每个标签上,将垂直内容压缩电阻设置为1000,将垂直内容设置为1000.这应允许标签自动调整大小并适合其内容。
  5. 在您的表格视图中(代码中)将estimatedRowHeight设置为您认为您的细胞将采取的某个值,并将rowHeight设置为UITableViewAutomaticDimension。这将告诉表格视图以单元高度为基础的行高。 (这是基于所有的东西它的内部高度转。有关更多信息,请参见this answer。)

更多的内容大小

Hector Matos这也解释了内容的概念检查它this article很好地拥抱优先和内容压缩抵抗。在其他的事情,文章包含这个有用的图形,从a tweet

Compression Resistance vs Content Hugginh

+0

嗨,内容拥抱和压缩工作,但我越来越奇怪的图像对齐问题。你能给些建议么?查看帖子https://imgur.com/a/zaNvP中的图片 – Kartik

1
  • 点击进入表格视图单元格
  • ⌘A
  • (图像视图和标签外)选择从右下角或菜单栏菜单Resolve Auto Layout Issues

弹出菜单enter image description here

+0

查看或故事板? – Kartik

+0

实际上单元格,你必须选择单元格的所有(橙色边框)UI元素 – vadian

+0

我试过了。该选项对我无效。查看我的帖子以获取更新的屏幕截图 – Kartik