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

卡迪

我有一个表格,其中包含由 nib 文件制成的自定义单元格。当用户单击一个单元格时,它会扩展 height ,从而模拟下拉列表。在自定义单元格中有一个 imageView,当单元格没有被点击时,它有一个下拉图像。但是,当单击单元格时,它应该将图像更改为折叠图像或向上箭头图像以显示单元格已打开。

当单元格从向下箭头扩展到向上箭头时,我在更改图像时遇到问题,反之亦然。我想要帮助实现这一点。

这是我的代码:

视图控制器.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;

视图控制器.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;

}

客户表Viewcell.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

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

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

在此处输入图片说明

特朗杜克

您应检查和更换dropDownImagecellForRowAtIndexPath方法。喜欢下面的代码

-(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;
}

更改ARROW_UPARROW_DOWN使用您的图像名称。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TableView 自定义单元格中的自定义 UIView 在表格加载时“跳跃”

自定义表格视图单元格图像失真

swift 5 自定义单元格图像单击

UITableview:单击自定义按钮时动态更改单元格高度

滚动时自定义单元格图像变形

在自定义渲染中单击单元格时,bootstrap vue 获取行数据

如何在带有分页的 tableview 自定义单元格中更改 imageview 的图像。

更改自定义单元格中按钮的图像

单击单元格时列表视图更改图标

如何在自定义表格视图单元格中创建自定义视图?

具有大图像和标签的自定义表格视图单元格

当在同一单元格中单击按钮时,如何在自定义tableview单元格中隐藏/显示特定视图

更改自定义表格单元格上的Leading / trailingSwipeActions的高度

如何在“表格”单元格中更改UIImageView图像,请单击该单元格上的按钮?

是否可以在不创建自定义单元格类的情况下更改单元格的图像?

Swift:-在自定义tableview单元格中单击按钮时将标签值增加1

Tornadofx自定义表格单元格

通过自定义表格单元格设置值

自定义表格单元格负载为空白

使用UIView的自定义表格视图单元格

表格视图单元格之间的自定义视图

使用 UIImagePickerController 更改自定义单元格的图像

使用其他单元格中的值在Google表格中创建自定义URL

自定义表格视图单元格中UILabel中的行数

从 html 表格中的多个单元格中减去自定义金额

自定义集合视图单元格添加图像

来自笔尖的表格视图单元格中的集合视图

如何在表格视图单元格中隐藏自定义类标签?

在自定义表格视图单元格中本地化按钮和标签[swift]