我正在使用 TableView 并希望自定义 TableViewCell 有一个小图像、名称和一个自定义图像(带 Tickmark 和不带 Tick)可以在附件上显示是否选择了 Cell,如果它没有被选中,它将在未选中时显示没有 Tick 图像细胞。如果我想选择多个单元格,那么它应该在选定的单元格上显示 Tick 图像,在未选定的单元格上显示 Untick 图像,然后当我点击一个按钮时,我应该能够获得选定的单元格 ID。
在 tableView 上,我从服务器获取所有值,也从 URL 获取图像,但 Tickmark 和 Unselected Tick mark 图像将用于项目本身。
到目前为止,我已经创建了:UITableViewCell 类型的“ResultTableCell”的类 .h、.m、.xib 和我的主视图“Result”,上面有 TableView 和一个按钮(点击按钮我会得到选中的值细胞)
结果表单元格.h
#import <UIKit/UIKit.h>
@interface ResultTableCell : UITableViewCell
@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@property (nonatomic, retain) IBOutlet UIImageView *thumbImageView;
结果表单元格.m
#import "ResultTableCell.h"
@implementation ResultTableCell
@synthesize nameLabel,thumbImageView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
ResultTableCell.xib xib 上的右侧图像是访问器图像的位置。 结果表单元格.xib
和主要的xib Result.h
#import <UIKit/UIKit.h>
#import "ASIFormDataRequest.h"
@interface Results : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *nameData;
}
@property (nonatomic, retain)NSMutableArray *nameData;
@property (nonatomic, retain)NSMutableArray *ImageData;
@property (nonatomic, retain)NSMutableArray *idData;
@property (nonatomic, retain)UITableView *table;
@property (nonatomic, retain) IBOutlet UIButton *done;
@property (nonatomic, retain) NSMutableArray *arFors;
-(IBAction)save_done:(id)sender;
结果.m
#import "Results.h"
#import "ResultTableCell.h"
@interface Results ()
@end
@implementation Results
@synthesize arFors;
@synthesize done,nameData,table,addressData,ImageData,idData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.arFors=[NSMutableArray array];
// I am Getting Name,id and image url data from my HomeViewController
NSLog(@"Name Data from home view is %@",nameData); // 10 Names get's printed in log
NSLog(@"id Data is %@",idData);
NSLog(@"URL image data is %@",ImageData);
table = [[UITableView alloc]initWithFrame:CGRectMake(0, 221, 320, 327) style:UITableViewStylePlain];
table.delegate = self;
table.dataSource = self;
[self.view addSubview:table];
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"Name data count is %d",nameData.count);
return nameData.count;
//return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/* UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
}*/
static NSString *simpleTableIdentifier = @"ResultTableCell";
ResultTableCell *cell = (ResultTableCell *)[table dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ResultTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if ([self.arFors containsObject:[NSNumber numberWithInt:indexPath.row]]) {
cell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:@"table_tick" ]];
}
else {
cell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:@"table_add" ]];
}
NSLog(@"data is ************* %@",nameData);
cell.nameLabel.text = [nameData objectAtIndex:indexPath.row];
NSURL * imageURL = [NSURL URLWithString:[ImageData objectAtIndex:indexPath.row]];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image2 = [UIImage imageWithData:imageData];
cell.ImageView.image = image2;
cell.ImageView.contentMode = UIViewContentModeScaleAspectFit;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"didSelectRowAtIndexPath %d",indexPath.row);
if ([self.arFors containsObject:[NSNumber numberWithInt:indexPath.row]]) {
[self.arFors removeObject:[NSNumber numberWithInt:indexPath.row]];
}
else{
[self.arFors addObject:[NSNumber numberWithInt:indexPath.row]];
// [self.table cellForRowAtIndexPath:indexPath];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[tableView reloadData];
}
-(IBAction)save_done:(id)sender
{
NSLog(@"selected cell values are %@",self.arFors);
}
现在使用此代码,一切正常(勾选图像显示在选定的单元格上,取消勾选图像在未选定的单元格上,单击完成按钮我得到选定的单元格值),
但是当我点击一个单元格时,问题就来了,然后它就像挂起并需要 5-6 秒的时间来更改访问器图像,因为它在 didselectrowatindexpath 方法中触发[tableView reloadData],因此所有数据在 tableview 中再次重新加载,然后访问器图像发生变化,请任何人都可以更正我的代码或增强它以使其快速运行。我尝试了很多方法,但如果不重新加载表格,我就无法做到,如果我重新加载表格,则需要很长时间。编码帮助将非常沉重。