单击由笔尖制成的自定义表格单元格中的更改图像
Posted
技术标签:
【中文标题】单击由笔尖制成的自定义表格单元格中的更改图像【英文标题】:Change image on click in custom table cell made from nib 【发布时间】:2017-12-12 00:48:46 【问题描述】:我有一个表格,其中包含由 nib 文件制成的自定义单元格。当用户单击一个单元格时,它会扩展 height ,从而模拟一个下拉菜单。在自定义单元格中有一个 imageView,当未单击该单元格时,该 imageView 具有一个下拉图像。但是,当单击单元格时,它应该将图像更改为折叠图像或向上箭头图像以显示单元格已打开。
当单元格从向下箭头扩展到向上箭头时,我在更改图像时遇到问题,反之亦然。我想要实现这一目标的帮助。
这是我的代码:
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
- (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
@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];
【问题讨论】:
可以加didExpandCell
方法吗?
嗨 @trungduc 它在 ViewController.m 中
在 didReceiveMemoryWarning 之后
好的,我明白了。我已经添加了答案,你可以看看
【参考方案1】:
您应该检查并更改 dropDownImage
中的 cellForRowAtIndexPath
方法。喜欢下面的代码
-(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_UP
和 ARROW_DOWN
更改为您的图像名称。
【讨论】:
非常感谢你,它就像一个魅力,你的答案总是很容易理解并且非常有效,如果有我可以关注的博客。 @kudzaimhou 实际上,我找不到适合您的 Objective-C 博客教程,但在我看来,您可以通过添加cell.selectionStyle = UITableViewCellSelectionStyleNone;
并使用 UITableViewRowAnimationFade
而不是 @987654328 来制作更好的动画@.
如果我想更改标签的颜色,当单元格展开时,这种方法是否有效。
您可以在cellForRowAtIndexPath
中更改它,就像我在dropDownImage
中所做的那样。
欢迎@kudzaimhou ;)以上是关于单击由笔尖制成的自定义表格单元格中的更改图像的主要内容,如果未能解决你的问题,请参考以下文章
iOS:如何从 tableview 的自定义单元格中的 UITextField 访问值
自定义 UITableView 单元格中的 UILabel 未随核心数据更改而更新
如何使用分页更改 tableview 自定义单元格中的 imageview 图像。