2017-08-03 34 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

相關問題