我想知道如何在UITableView
中的选定单元格行中设置图像。
如果我选择第一个单元格,图像应显示在第一个单元格中,稍后当我选择第二个单元格时,图像应仅显示在第二个单元格中(而不是第一个单元格)
基本上,图像应仅显示在所选单元格中。
答案 0 :(得分:2)
最干净的方法是继承UITableViewCell。
#import <UIKit/UIKit.h>
@interface SongCell : UITableViewCell
@property (nonatomic, assign) BOOL isPlaying;
@end
@interface SongCell ()
@property (nonatomic, strong) UIImage *speakerImage;
@end
@implementation SongCell
-(void)layoutSubviews
{
if (!_speakerImage) {
// http://commons.wikimedia.org/wiki/File:Speaker_Icon.svg
self.speakerImage = [UIImage imageNamed:@"speaker_icon.png"];
}
if(!self.isPlaying){
self.imageView.image = nil;
} else {
self.imageView.image = _speakerImage;
self.imageView.hidden =NO;
}
[super layoutSubviews];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if (!selected) {
self.isPlaying = NO;
}
}
-(void)setIsPlaying:(BOOL)isPlaying
{
_isPlaying = isPlaying;
[self setNeedsLayout];
}
@end
UITableViews数据源和委托实现
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 20;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"SongCell";
SongCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SongCell *cell =(SongCell *)[tableView cellForRowAtIndexPath:indexPath] ;
cell.isPlaying = !cell.isPlaying;
}
我为你准备了一个演示项目。您可以在GitHub
找到它答案 1 :(得分:0)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
[UIView animateWithDuration:2.0f animations:^{
[imageView setHidden:NO];
} completion:^(BOOL finished) {
;
}];
}
答案 2 :(得分:0)
您想要imageView
的切换类型。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
[cell.imageView setImage: [UIImage imageNamed:@""]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//refresh all cells
//basically call cellForRowAtIndexPath to reset all cells
[tableView reloadSections:indexPath.section
withRowAnimation:UITableViewRowAnimationFade];
//get cell for the currently selected row
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//set image
[cell.imageView setImage: [UIImage imageNamed:@"loudspeaker.png"]];
}
但这只是一个视觉方面
如果您选择第一行并向下滚动并稍后返回第一行,则图片将不再存在,因为-cellForRowAtIndexPath:
已被召回,而且由于我们已[cell.imageView setImage: [UIImage imageNamed:@""]];
,imageView.image
重置。
要处理上述情况,您需要记住选择了哪些行。 (您可以通过基本数组实现此目的)
- (void)viewDidLoad {
[super viewDidLoad];
//arrMyDataSource is defined in .h as 'NSArray *arrMyDataSource;'
//needless to say but this is the tableView's datasource contents
arrMyDataSource = @[@"Apple",@"Banana",@"Candy",@"Door",@"Elephant"];
//arrMemoryMap is defined in .h as 'NSMutableArray *arrMemoryMap;'
//this is one way to remember which row is selected
arrMemoryMap = [NSMutableArray alloc] init];
for (int i = 0 ; i < arrMyDataSource.count ; i++) {
NSNumber *tmpSelectStatus = [NSNumber numberWithBool:NO];
[arrMemoryMap addObject: tmpSelectStatus];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//...
if([[arrMemoryMap objectAtIndex:indexPath.row] boolValue] == YES) {
[cell.imageView setImage: [UIImage imageNamed:@"loudspeaker.png"]];
} else {
[cell.imageView setImage: [UIImage imageNamed:@""]];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//first, reset arrMemoryMap for all rows
for (int i = 0 ; i < arrMemoryMap.count ; i++) {
if([[arrMemoryMap objectAtIndex:i] boolValue] == YES]) {
[[arrMemoryMap objectAtIndex:i] setBoolValue: NO];
break;
}
}
//now, set current row as selected in arrMemoryMap
[[arrMemoryMap objectAtIndex:indexPath.row] setBoolValue: YES];
//reload data or particular section
[tableView reloadSections:indexPath.section
withRowAnimation:UITableViewRowAnimationFade];
}