gpt4 book ai didi

objective-c - 自定义 TableView-Cell(来自服务器的数据)通过附件更改选择多个值

转载 作者:搜寻专家 更新时间:2023-10-30 19:44:00 24 4
gpt4 key购买 nike

我正在使用 TableView 并希望自定义 TableViewCell 有一个小图像、名称和一个自定义图像(带刻度标记和不带刻度)可以在附件上显示是否选择了单元格,如果没有选择它会显示没有刻度未选择的单元格上的图像。如果我想选择多个单元格,那么它应该在选定的单元格上显示勾选图像,在未选定的单元格上显示取消勾选图像,然后当我点击一个按钮时,我应该能够获得选定的单元格 ID。

在 tableView 上,我从服务器获取所有值,也从 URL 获取图像,但 Tickmark 和 Unselected Tick mark 图像将用于项目本身。

到目前为止我已经创建了:UITableViewCell 类型的“ResultTableCell”的 .h、.m、.xib 类和我的主视图“Result”,顶部有 TableView 和一个按钮(单击按钮我将获得所选单元格的值)

ResultTableCell.h

#import <UIKit/UIKit.h>

@interface ResultTableCell : UITableViewCell

@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@property (nonatomic, retain) IBOutlet UIImageView *thumbImageView;

ResultTableCell.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.xibxib 上的右侧图像是访问器图像所在的位置。 ResultTableCell.xib

和主要的xibResult.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 中,然后访问器图像发生变化,请任何人都可以更正我的代码或增强它以使其快速运行。我已经尝试了很多方法,但如果不重新加载表格我就无法做到这一点,如果我重新加载表格需要很长时间。编码帮助将非常丰富。

最佳答案

你的问题是:

NSData * imageData = [NSData dataWithContentsOfURL:imageURL];

因为每次重新加载表格 View 时它都会从网络下载所有图像数据。您应该异步执行此操作并缓存返回的图像,这样您就不需要重复下载它。看看像 SDWebImage 这样的图书馆来帮助您。

关于objective-c - 自定义 TableView-Cell(来自服务器的数据)通过附件更改选择多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18227623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com