Cell Row Height基于JSON解析图像

时间:2014-02-14 16:06:11

标签: ios objective-c json uitableview

我正在从服务器上的JSON文件中解析图像。现在我将heightForRowAtIndexPath设置为返回389.但是现在有些图像被拉伸了。我需要单元格根据图像高度调整它的大小。现在,我的代码看起来像这样:

@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 btnTwitter.selected?@"title":@"title";
}

- (NSString *)getPostedKey
{
    return btnTwitter2.selected?@"uploaded":@"published";
}

- (NSString *)getTwitterName
{
    return btnTwitter.selected?@"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

1 个答案:

答案 0 :(得分:0)

首先你需要一些变量来保存图像高度 - 数组或字典,然后当你得到图像时保存高度并调用[tableView reloadRowAtIndexPath:indexPath] 并在

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

之前返回正确的高度