2016-04-28 4 views
0

私はiOS開発の新人です。これは初めてのプロジェクトです。私はuicollectionviewを使って、私の大学の非公式fbページのフィードを表示しようとしています。私はさまざまなことを試みてきましたが、何も動かないようです。 UICollectionViewController.mファイルコレクションビューのセル間に不規則なスペースがあります

- (void)viewDidLoad { 
[super viewDidLoad]; 
self.title = @"DCE Speaks Up"; 
self.collectionView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1]; 
// Uncomment the following line to preserve selection between presentations 
// self.clearsSelectionOnViewWillAppear = NO; 
NSError *error= nil; 

NSURL *feedURL = [NSURL URLWithString:@"https://graph.facebook.com/382057938566656/feed?fields=id,full_picture,message,story,created_time,link&access_token=1750413825187852%7CkUl9nlZFPvdGxGZX4WYabKKG2G4"]; 

NSData *jsonData = [NSData dataWithContentsOfURL:feedURL]; 

NSDictionary *dataDictionary= [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; 
self.feedArray = [NSMutableArray array]; 
NSArray *feedTempArray = [dataDictionary objectForKey:@"data"];  //feedTempArray just used for parsing 

for (NSDictionary *feedDictionary in feedTempArray) { 
    FacebookFeed *fbFeed =[FacebookFeed facebookFeedWithMessage:[feedDictionary objectForKey:@"message"]]; 
    fbFeed.story = [feedDictionary objectForKey:@"story"]; 
    fbFeed.imageURL = [NSURL URLWithString:[feedDictionary objectForKey:@"full_picture"]]; 
    fbFeed.date = [feedDictionary objectForKey:@"created_time"]; 
    fbFeed.sharedLink = [NSURL URLWithString:[feedDictionary objectForKey:@"link"]]; 
    [self.feedArray addObject:fbFeed]; 
} 


// Register cell classes 
[self.collectionView registerClass:[FacebookCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier]; 

// Do any additional setup after loading the view. 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
FacebookCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 
cell.feed = self.feedArray[indexPath.row]; 
cell.sizeDictionary = [self sizeForLabelAtIndexPath:indexPath.row collectionView:collectionView]; 
// Configure the cell 
for (UIView *view in cell.contentView.subviews) { 
    if ([view isKindOfClass:[UILabel class]]) { 
     [view removeFromSuperview]; 
    }else if ([view isKindOfClass:[UIImageView class]]){ 
     [view removeFromSuperview]; 
    } 
} 
    cell.layer.shouldRasterize = YES;   
    cell.layer.rasterizationScale = [UIScreen mainScreen].scale; 
return cell; 
} 
#pragma mark <UICollectionViewDelegateFlowLayout> 
-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    NSDictionary *sizeDictionary = [[NSDictionary alloc] init]; 
    sizeDictionary = [self sizeForLabelAtIndexPath:indexPath.row collectionView:collectionView]; 
     return CGSizeMake(self.view.frame.size.width - 20, [sizeDictionary[@"imageHeight"] floatValue] + [sizeDictionary[@"messageHeight"] floatValue] + [sizeDictionary[@"nameHeight"] floatValue]); 
} 
#pragma mark <UICollectionViewDelegate> 
- (NSDictionary *) sizeForLabelAtIndexPath:(NSUInteger)indexPath collectionView:(UICollectionView *)collectionView{ 
FacebookFeed *feed = self.feedArray[indexPath]; 
CGSize nameSize = CGSizeFromString(@"DCE Speaks Up"); 
CGSize imageSize = CGSizeMake(0, 0); 
if (feed.imageURL) { 
    imageSize = CGSizeMake(470, 394); 
} 
float height = 80; 
NSString *string = @""; 
if (feed.message) { 
    string = [feed.message stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
}else if (feed.story){ 
    string = [feed.story stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
} 
if (string) { 
    NSDictionary *attributes = @{ 
           NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleBody] 
           }; 
    CGRect bodyFrame = 
    [string boundingRectWithSize:CGSizeMake(CGRectGetWidth(collectionView.bounds), 
              CGFLOAT_MAX) 
         options:(NSStringDrawingUsesLineFragmentOrigin) 
         attributes:attributes 
         context:nil]; 

    height += ceilf(CGRectGetHeight(bodyFrame)); 
} 
return @{ @"imageHeight" : @(imageSize.height) , @"messageHeight" : @(height) , @"nameHeight" : @(nameSize.height)}; 
} 

カスタムセルファイル

@implementation FacebookCollectionViewCell 

- (instancetype)initWithFrame:(CGRect)frame{ 
self = [super initWithFrame:frame]; 
if (self) 
{ 
    self.contentView.backgroundColor = [UIColor whiteColor]; 
    // change to our custom selected background view 
    self.imageView = [[UIImageView alloc] init]; 
    self.messageLabel = [[UILabel alloc] init]; 
    self.nameLabel = [[UILabel alloc] init]; 
} 
return self; 
} 

-(void) setFeed:(FacebookFeed *)feed{ 
_feed = feed; 
self.messageLabel.numberOfLines = 0; 
self.messageLabel.lineBreakMode = 0; 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    NSData *data= [NSData dataWithContentsOfURL:feed.imageURL]; 
    UIImage *theImage=[UIImage imageWithData:data]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     self.imageView.image= theImage ; 
    }); 
}); 
if (feed.message) { 
    self.messageLabel.text = [[NSString alloc] initWithString:[feed.message stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; 
}else{ 
    self.messageLabel.text = [[NSString alloc] initWithString:[feed.story stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; 
} 
self.messageLabel.font = [UIFont fontWithName:@"ArialMT" size:11]; 
[self.messageLabel sizeToFit]; 
self.nameLabel.text = @"DCE Speaks Up"; 
[self.nameLabel setFont:[UIFont fontWithName:@"ArialMT" size:12]]; 
[self.nameLabel sizeToFit]; 
} 
-(void) layoutSubviews{ 
NSLog(@"%@",self.feed); 
self.nameLabel.frame = CGRectMake(10, 10, self.contentView.bounds.size.width, [self.sizeDictionary[@"nameHeight"] floatValue]); 
float height = 200; 
if (self.contentView.bounds.size.height - [self.sizeDictionary[@"messageHeight"] floatValue] -[self.sizeDictionary[@"nameHeight"] floatValue] >100) { 
    height = self.contentView.bounds.size.height - [self.sizeDictionary[@"messageHeight"] floatValue] -[self.sizeDictionary[@"nameHeight"] floatValue]; 
} 
if (self.feed.imageURL) { 
    self.imageView.frame = CGRectMake(10, [self.sizeDictionary[@"messageHeight"] floatValue] +[self.sizeDictionary[@"nameHeight"] floatValue], self.contentView.bounds.size.width - 20,height); 
    self.messageLabel.frame = CGRectMake(10, [self.sizeDictionary[@"nameHeight"] floatValue] +10, self.contentView.bounds.size.width - 10, [self.sizeDictionary[@"messageHeight"] floatValue]); 

    [self.contentView addSubview:self.messageLabel]; 
    [self.contentView addSubview:self.imageView]; 

}else{ 
    self.messageLabel.frame = CGRectMake(10, [self.sizeDictionary[@"nameHeight"] floatValue], self.contentView.bounds.size.width - 10, [self.sizeDictionary[@"messageHeight"] floatValue]); 
    [self.contentView addSubview:self.messageLabel]; 
} 
[self.contentView addSubview:self.nameLabel]; 

} 
@end 

enter image description here enter image description here

+0

これは1時間前の3番目の質問と約4時間前の1番目の質問です。これらの質問はすべて同じようです。なぜこれを何度も繰り返し投稿していますか? – rmaddy

+0

実際に私は問題の全セットを含む1つの質問を投稿しましたが、それはあいまいであるためにdownvotedでしたので、3つの質問に質問を破り、質問は異なっています –

+0

それは縦レイアウトの場合、 –

答えて

1

使用NSLayoutConstraint sが、それが標準とベストプラクティスです。また、このメソッドが複数回呼び出される可能性があるので、layoutSubviews()にサブビューを追加しないでください。また、あなたのセルサイズ計算コードをUICollectionViewCellサブクラスに移動することをお勧めします。

あなたのコードには奇妙なことがたくさんあるので、あなたの問題の原因を教えにくいです。おそらく、サブビュー追加/削除とマニュアルサイズ計算のすべての組み合わせです。また、あなたに起こっかもしれないものについてのいくつかの手がかりを与えることを視覚的デバッガを使用することができます。 私はあなたの問題はここにあると信じてhttps://www.raywenderlich.com/98356/view-debugging-in-xcode-6

0

self.view.frame.size.width - 20 
:あなたはCGSizeMake幅に設定

-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ 

    NSDictionary *sizeDictionary = [[NSDictionary alloc] init]; 
    sizeDictionary = [self sizeForLabelAtIndexPath:indexPath.row collectionView:collectionView]; 
    return CGSizeMake(self.view.frame.size.width - 20, [sizeDictionary[@"imageHeight"] floatValue] + [sizeDictionary[@"messageHeight"] floatValue] + [sizeDictionary[@"nameHeight"] floatValue]); 
} 

と高さ:

[sizeDictionary[@"imageHeight"] floatValue] + [sizeDictionary[@"messageHeight"] floatValue] + [sizeDictionary[@"nameHeight"] floatValue] 

場合は、それをチェックアウトあなたの身長のサイズは正しいです。それは大きすぎるように見えます。このメソッドは、各セルのサイズを返します。私はあなたが望むよりも大きな身長を設定していると信じています。

関連する問題