简体   繁体   English

单击由笔尖制成的自定义表格单元格时更改图像

[英]Change image on click in custom table cell made from nib

I have a table with custom cells that are made from a nib file. 我有一张表格,其中包含由nib文件制成的自定义单元格。 When a user clicks a cell it expands in height , thus simulating a dropdown. 用户单击单元格时,它的高度会扩展,从而模拟下拉菜单。 In the custom cell there is an imageView that has a dropDown image when the cell is not clicked. 在自定义单元格中,当未单击该单元格时,有一个imageView带有dropDown图像。 However when the cell is clicked it should change the image to a collapse image or arrow up image to show that the cell is open. 但是,单击单元格时,应将图像更改为折叠图像或向上箭头图像,以显示该单元格处于打开状态。

I am having a problem changing the image when the cell expands from arrow down to arrow up and vice versa. 当单元格从向下箭头扩展到向上箭头,反之亦然时,更改图像时出现问题。 I would like assistance achieving this. 我希望获得帮助。

Here is my code : 这是我的代码:

ViewController.h ViewController.h

@interface ViewController : UIViewController <UITableViewDelegate, 

UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic, strong) NSArray *items;
@property (nonatomic, assign) BOOL expandFlag;
@property (nonatomic, assign) NSIndexPath* selectedIndex;

ViewController.m ViewController.m

- (NSArray *) items
{
    if (!_items) {
        _items = [NSArray new];
    }
    return _items;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    UINib *nib = [UINib nibWithNibName:@"CustomTableViewCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"cell"];

    _items = @[
                        @{@"title":@"Simpson", @"short":@"Homer", @"long":@"Mr jhvjm,b;k t  tyfy rctrc rtcf rvty rthvgh bb r5ertvyg hg r5 tyhg 6ruygj 8rutyj r6yiugg tygdtjhgfdt hg tvt gthgfgni7yjftgjhb ftgfh b yjh gtfjfyhh j jj j", @"image":@"There are many ways to create expandable cells in the table view. Few of them you can easily find on this blog or somewhere in Google. One of that is the official Apple “Date cell” demo code. However, most of that describing the little hard way by using operations directly on constraints."},
                        @{@"title":@"Simpson", @"short":@"Marge", @"long":@"Mrs bjyvhm uikn o utv jb k", @"image":@"There are many ways to create expandable cells in the table view. Few of them you can easily find on this blog or somewhere in Google. One of that is the official Apple “Date cell” demo code. However, most of that describing the little hard way by using operations directly on constraints."},
                        @{@"title":@"Simpson", @"short":@"Bart", @"long":@"Mr vubj cbjknuy  iubyuvjh biubkj ", @"image":@"There are many ways to create expandable cells in the table view. Few of them you can easily find on this blog or somewhere in Google. One of that is the official Apple “Date cell” demo code. However, most of that describing the little hard way by using operations directly on constraints."},
                        @{@"title":@"Simpson", @"short":@"Lisa", @"long":@"Miss jbjvjbbiuvu yuvhj uby ", @"image":@"There are many ways to create expandable cells in the table view. Few of them you can easily find on this blog or somewhere in Google. One of that is the official Apple “Date cell” demo code. However, most of that describing the little hard way by using operations directly on constraints."},
                        @{@"title":@"Simpson", @"short":@"Maggie", @"long":@"Miss iubniyujh k iuuh  ", @"image":@"There are many ways to create expandable cells in the table view. Few of them you can easily find on this blog or somewhere in Google. One of that is the official Apple “Date cell” demo code. However, most of that describing the little hard way by using operations directly on constraints."},
                        @{@"title":@"Flanders", @"short":@"Ned", @"long":@"Mr hbuyvj iybkj nui  uhc n", @"image":@"There are many ways to create expandable cells in the table view. Few of them you can easily find on this blog or somewhere in Google. One of that is the official Apple “Date cell” demo code. However, most of that describing the little hard way by using operations directly on constraints."}
                        ];
    // Do any additional setup after loading the view, typically from a nib.
}


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

-(void) didExpandCell{

    _expandFlag = !_expandFlag;
    [self.tableView reloadRowsAtIndexPaths:@[_selectedIndex] withRowAnimation:UITableViewRowAnimationAutomatic];

}

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

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

    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSDictionary *item = _items[indexPath.row];
    cell.titleImage.text = [item objectForKey:@"title"];
    cell.longLabel.text = [item objectForKey:@"long"];
    cell.shortLabel.text = [item objectForKey:@"image"];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    _selectedIndex = indexPath;
    [self didExpandCell];
}

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

    if (_expandFlag && _selectedIndex == indexPath) {
        return 400;
    }
    return 200;

}

CustomeTableViewcell.h CustomeTableViewcell.h

@interface CustomTableViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *thumbImage;
@property (weak, nonatomic) IBOutlet UILabel *titleImage;
@property (weak, nonatomic) IBOutlet UILabel *shortLabel;
@property (weak, nonatomic) IBOutlet UILabel *longLabel;
@property (weak, nonatomic) IBOutlet UIImageView *dropDownImage;

CustomerTableViewCell.m CustomerTableViewCell.m

- (void)awakeFromNib {
    [super awakeFromNib];
}

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

在此处输入图片说明

You should check and change dropDownImage in cellForRowAtIndexPath method. 您应该检查并更改cellForRowAtIndexPath方法中的dropDownImage Something likes the code below 像下面的代码

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

  CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
  NSDictionary *item = _items[indexPath.row];
  cell.titleImage.text = [item objectForKey:@"title"];
  cell.longLabel.text = [item objectForKey:@"long"];
  cell.shortLabel.text = [item objectForKey:@"image"];

  NSString *dropDownImageName = [indexPath isEqual:_selectedIndex] && _expandFlag ? @"ARROW_DOWN" : @"ARROW_UP";
  cell.dropDownImage.image = [UIImage imageNamed:dropDownImageName];

  return cell;
}

Change ARROW_UP and ARROW_DOWN with your image names. 用您的图像名称更改ARROW_UPARROW_DOWN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM