2016-03-11 35 views
0

我在包含标签和imageView的高度为300的故事板中制作了一个自定义单元格,但对于某些情况下,如果我没有收到响应图像,我想降低单元格高度并想从单元格中移除imageView因此用户无法看到它。 在这种情况下,我只想显示标签中的文字。 我怎么能做到这一点?我知道这个愚蠢的问题,但我被困在这里。 您的帮助,将不胜感激。 谢谢如何在Objective C中减少运行时UITableViewCell的大小?

+0

只需在heightForRowAtIndexPath上添加条件即可。 如果图像不存在然后降低高度 –

+0

但在imageview下我也有按钮...如果我降低高度它也会隐藏它。 –

+0

然后,您需要更改cellforrowatindexpath中按钮的框架。相应地减少按钮的y值。 –

回答

0

我已经写了一些基本的代码来了解如何表视图单元格调整大小工作(不相关的自定义或基本单元格)。

@interface MasterViewController() 

@property NSArray *objects; 
@end 

@implementation MasterViewController 

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

    _objects = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ImageList" ofType:@"plist"]]; 
} 


- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table View 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return _objects.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

    NSDictionary *object = _objects[indexPath.row]; 
    cell.textLabel.text = object[@"text"]; 
    cell.imageView.image = [UIImage imageNamed:object[@"image"]]; 
    return cell; 
} 

//- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
// return _objects.allKeys[section]; 
//} 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return [_objects[indexPath.row][@"image"] length] ? 60.0f : 30.0f; 
} 

@end 

在上面的代码中,你首要重点应放在方法heightForRowAtIndexPath,它的代码行return [_objects[indexPath.row][@"image"] length] ? 60.0f : 30.0f;

现在好像您从服务器动态加载图像,然后跟踪已加载哪个单元的图像,并呼叫[tableView reloadRowsAtIndexPaths:<#(nonnull NSArray<NSIndexPath *> *)#> withRowAnimation:<#(UITableViewRowAnimation)#>]以及相关索引路径。同时请确保您在dataSource中更新image loaded信息,即上述代码中的_objects

我已经上传code here。在工作区检查UITableViewTest