根据UIImageView动态获取单元格的高度

时间:2012-10-22 12:03:03

标签: uitableview uiimageview

我有一些照片。我的桌面视图会加载它们并根据图片的高度更改行的高度。我的图片大小不同。

- (void)viewDidLoad
{

    NSString *imageName=[kDataArray objectAtIndex:(arc4random()%[kDataArray count])];
    UIImageView *importIMG=[[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];


    //init tablexiewcell
    arrayList=[[NSMutableArray alloc] initWithCapacity:kRowsInitSize];
    for (NSUInteger i=0; i<kRowsInitSize; i++) {
        [arrayList addObject:[kDataArray objectAtIndex:rand()%2]];
    }

    [TableView setPullToRefreshHandler:^{

        NSArray * newRows=[NSArray arrayWithObject:[kDataArray objectAtIndex:rand()%2]];
        [self performSelector:@selector(dataDidRefresh:) withObject:newRows afterDelay:3.0];

    }];

[super viewDidLoad];
}

#pragma mark- UITableViewDataSource


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCell* cell=[self tableView:tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"%f",cell.Pimage.frame.size.height);
    return cell.Pimage.frame.size.height;

}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [arrayList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    NSString *imageName=[kDataArray objectAtIndex:(arc4random()%[kDataArray count])];
    UIImageView *importIMG=[[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];

    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell==nil) {
        cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.Pimage=importIMG;

    }
    return cell;
}

#pragma mark - Private methods

-(void)dataDidRefresh:(NSArray *)data
{
    [TableView refreshFinished];

    NSIndexSet * indexSet=[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [data count]) ];
    NSLog(@"%d",[data count]);
    [arrayList insertObjects:data atIndexes:indexSet];

    NSMutableArray * indexPaths=[[NSMutableArray alloc] initWithCapacity:[data count]];
    for (NSUInteger i=0; i<[data count]; i++)
    {
        [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
    }
    [TableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
    [indexPaths release];
}

我写了这些代码,但我没有实现我的目标。我怎么编码呢?请给我帮助!!!

0 个答案:

没有答案