2014-02-14 65 views
0

我從我的服務器上的JSON文件解析圖像。現在我將heightForRowAtIndexPath設置爲返回389.但是現在一些圖像已經被拉伸了。我需要單元格根據圖像高度調整它的大小。現在,我的代碼如下所示:基於JSON解析圖像的單元行高度

@implementation Pictures 
@synthesize tableView = _tableView, activityIndicatorView = _activityIndicatorView; 
@synthesize btnFaceBook, btnTwitter, btnTwitter2; 
@synthesize strURLToLoad; 
@synthesize movies; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; 
    [refreshControl setTintColor:[UIColor greenColor]]; 
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged]; 
    [self.tableView addSubview:refreshControl]; 


    strURLToLoad = [[NSMutableString alloc] init]; 

    [btnFaceBook setTitle:@"link-to-json.com/json.php" forState:UIControlStateDisabled]; 
    [btnTwitter setTitle:@"link1-to-json.com/json.php" forState:UIControlStateDisabled]; 
    [btnTwitter2 setTitle:@"link2-to-json.com/json.php" forState:UIControlStateDisabled]; 

    [btnFaceBook setBackgroundImage:[UIImage imageNamed:@"tab_selected.png"] forState:UIControlStateNormal]; 
    [btnFaceBook setBackgroundImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateSelected]; 

    [btnTwitter setBackgroundImage:[UIImage imageNamed:@"tab_selected.png"] forState:UIControlStateNormal]; 
    [btnTwitter setBackgroundImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateSelected]; 

    [btnTwitter2 setBackgroundImage:[UIImage imageNamed:@"tab_selected.png"] forState:UIControlStateNormal]; 
    [btnTwitter2 setBackgroundImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateSelected]; 

    [self.navigationController setNavigationBarHidden:YES]; 

    self.tableView.separatorColor = [UIColor clearColor]; 

    // Setting Up Activity Indicator View 
    self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 

    self.activityIndicatorView.color = [UIColor greenColor]; 
    self.activityIndicatorView.hidesWhenStopped = YES; 
    self.activityIndicatorView.center = self.view.center; 
    [self.view addSubview:self.activityIndicatorView]; 
    [self.activityIndicatorView startAnimating]; 
    self.tableView.separatorColor = [UIColor clearColor]; 

    // Initializing Data Source 
    movies = [[NSMutableArray alloc] init]; 

    [self btnFromTabBarClicked:btnFaceBook]; 
} 

- (void)loadJSONFromCurrentURL 
{ 
    [self.activityIndicatorView startAnimating]; 

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:strURLToLoad]]; 

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
     [movies setArray:JSON]; 
     [self.activityIndicatorView stopAnimating]; 
     [self.tableView reloadData]; 

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
     NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo); 
    }]; 

    [operation start]; 
} 

-(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
} 

- (IBAction)btnFromTabBarClicked:(UIButton *)sender 
{ 
    //Unselect all 3 buttons 
    btnFaceBook.selected = btnTwitter.selected = btnTwitter2.selected = NO; 

    //Select the button that was clicked 
    sender.selected = YES; 

    //Set the string of an NSMutableString property called strURLToLoad with the URL 
    //The URL is pre stored in the text of the UIButton in the Disabled text. 
    [strURLToLoad setString:[sender titleForState:UIControlStateDisabled]]; 

    //Load the URL 
    [self loadJSONFromCurrentURL]; 
} 

- (IBAction)showButtonMenu { 
    [self.frostedViewController presentMenuViewController]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (self.movies && self.movies.count) { 
     return self.movies.count; 
    } else { 
     return 0; 
    } 
} 

//here I want the cell to adjust it's size based on the image height 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 389; 

} 

- (NSString *)getTextKey 
{ 
    return [email protected]"title":@"title"; 
} 

- (NSString *)getPostedKey 
{ 
    return [email protected]"uploaded":@"published"; 
} 

- (NSString *)getTwitterName 
{ 
    return [email protected]"celebtag":@"celebname"; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    if(indexPath.row == 0) { 
     static NSString *Identifier1 = @"TableHeaderView"; 


     TableHeaderView *cell = [tableView dequeueReusableCellWithIdentifier:Identifier1]; 
     if (cell == nil) { 
      NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"TableHeaderView" owner:self options:nil]; 
      cell = (TableHeaderView *)[nib objectAtIndex:0]; 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 

      cell.backgroundColor = [UIColor clearColor]; 
      return cell; 
     } 

    } else { 
    static NSString *simpleTableIdentifier = @"PicturesObject"; 

    PicturesObject *cell = (PicturesObject *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PicturesObject" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    NSDictionary *movie = [self.movies objectAtIndex:indexPath.row]; 
    NSString *strText = [movie objectForKey:[self getTextKey]]; 

    NSURL *url = [[NSURL alloc] initWithString:[movie objectForKey:@"link"]]; 
    [cell.pic setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]]; 
    cell.published.text = [movie objectForKey:[self getPostedKey]]; 
    cell.title.text = [movie objectForKey:@"title"]; 
    cell.twitterName.text = [movie objectForKey:[self getTwitterName]]; 

    return cell; 
    } return 0; 
} 

@end 
+0

它更復雜的做到這一點,使用固定大小的圖片查看每個細胞或獲得和網絡服務的高度 – NANNAV

回答

0

起初,你需要一些變量來保存圖像高度 - 數組或字典,然後當你保存圖像的高度,並呼籲[tableView reloadRowAtIndexPath:indexPath]

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

回報適當的高度

+0

好,而且我怎麼能做到這一點之前保存的? – tracifycray

+0

取決於您使用哪個庫下載圖像,其中大部分都有setImageWithURL varian:帶有完成處理程序塊,您可以從中獲取下載的圖像。 – sage444