Ios 如何使用自定义单元格将复选框添加到我的UITableView?
我有一个自定义单元格的UITableview。在那个单元格中,我有一个按钮,我计划将其用作复选框。这意味着单击按钮后,我会将图像更改为“记号”。再次选择它将更改为空按钮。我已经完成了这一部分,但问题是,由于UITablecell重复使用了该单元格,如果向下滚动,非选定单元格的勾号会再次出现。请帮助我用一个示例代码或逻辑来实现这一点。请注意,我不希望使用复选标记附件,这是UITable的默认设置Ios 如何使用自定义单元格将复选框添加到我的UITableView?,ios,objective-c,uitableview,tableviewcell,Ios,Objective C,Uitableview,Tableviewcell,我有一个自定义单元格的UITableview。在那个单元格中,我有一个按钮,我计划将其用作复选框。这意味着单击按钮后,我会将图像更改为“记号”。再次选择它将更改为空按钮。我已经完成了这一部分,但问题是,由于UITablecell重复使用了该单元格,如果向下滚动,非选定单元格的勾号会再次出现。请帮助我用一个示例代码或逻辑来实现这一点。请注意,我不希望使用复选标记附件,这是UITable的默认设置 - (UITableViewCell *)tableView:(UITableView *)t
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellIdentifier = @"cells";
cell = (MyWachizCustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.whiteCircle.tag = indexPath.row;
//cell=nil;
if(cell == nil)
{
cellLoadArray=[[NSBundle mainBundle]loadNibNamed:@"MyWachizCustomCell" owner:self options:nil];
if(tableView==self.tableMyFavourite)
{
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
cell=[cellLoadArray objectAtIndex:3];
}
else
{
cell=[cellLoadArray objectAtIndex:1];
NSLog(@"Index Path %ld",(long)indexPath.row);
}
if(indexPath.row%2==0)
cell.imgCellBg.backgroundColor=[UIColor whiteColor];
else
cell.imgCellBg.backgroundColor=[UIColor colorWithRed:0.92 green:0.91 blue:0.91 alpha:1.0];
}
else if (tableView==self.tableMySearch)
{
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
cell=[cellLoadArray objectAtIndex:2];
}
else
{
cell=[cellLoadArray objectAtIndex:0];
}
if(indexPath.row%2==0)
cell.imgCellBg.backgroundColor=[UIColor whiteColor];
else
cell.imgCellBg.backgroundColor=[UIColor colorWithRed:0.92 green:0.91 blue:0.91 alpha:1.0];
}
else if(tableView==self.tableMyAds)
{
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
cell=[cellLoadArray objectAtIndex:3];
}
else
{
cell=[cellLoadArray objectAtIndex:1];
}
if(indexPath.row%2==0)
cell.imgCellBg.backgroundColor=[UIColor whiteColor];
else
cell.imgCellBg.backgroundColor=[UIColor colorWithRed:0.92 green:0.91 blue:0.91 alpha:1.0];
}
}
cell.btnGreen.hidden=YES;
cell.btnAdsDeleteAction.hidden=YES;
self.btnMyFavoriteDeleteCheckBox.hidden=YES;
self.btnMySearchesDeleteCheckBox.hidden=YES;
self.btnMyAdsDeleteCheckBox.hidden=YES;
self.btnMyFavoriteDeleteAction.hidden=YES;
self.btnMySearchesDeleteAction.hidden=YES;
self.btnMyAdsDeleteAction.hidden=YES;
if(tableView==self.tableMyFavourite)
{
cell.btnGreen.hidden=NO;
cell.btnAdsDeleteAction.hidden=YES;
self.btnMyFavoriteDeleteCheckBox.hidden=NO;
self.btnMySearchesDeleteCheckBox.hidden=YES;
self.btnMyAdsDeleteCheckBox.hidden=YES;
self.btnMyFavoriteDeleteAction.hidden=NO;
self.btnMySearchesDeleteAction.hidden=YES;
self.btnMyAdsDeleteAction.hidden=YES;
// if(checkBoxTagValue==1)
// {
// //cell.btnGreen.selected=YES;
// cell.imgCheckbox.image=[UIImage imageNamed:@"markedmw.png"];
// }
// else
// {
// //cell.btnGreen.selected=NO;
// cell.imgCheckbox.image=[UIImage imageNamed:@"unmarkedmw.png"];
// }
if ([[[arrayMyFavourite objectAtIndex:indexPath.row] valueForKey:@"seller_verification"] isEqualToString:@"Yes"]) {
cell.imgverification.image=[UIImage imageNamed:@"verifiedIcon.png"];
cell.lblverification.text=@"Verified";
}
else {
cell.imgverification.image=nil;
cell.lblverification.text=@"";
}
if([[[arrayMyFavourite objectAtIndex:indexPath.row]valueForKey:@"favorite_id"] isKindOfClass:[NSNull class]])
{
cell.lblDeleteMyFavourite.text=@"";
}
else
{
for(NSString * string in self.arrayDynamicFavourite)
{
if([string isEqualToString:[[arrayMyFavourite objectAtIndex:indexPath.row]valueForKey:@"favorite_id"]])
cell.btnGreen.selected=YES;
}
cell.lblDeleteMyFavourite.text=[[arrayMyFavourite objectAtIndex:indexPath.row]valueForKey:@"favorite_id"];
//NSLog(@"%@",cell.lblDeleteMyFavourite.text);
}
//NSLog(@"%@",[[arrayMyFavourite objectAtIndex:indexPath.row]valueForKey:@"watch_name"]);
//NSLog(@"%@",[[arrayMyFavourite objectAtIndex:indexPath.row]valueForKey:@"watch_price"]);
// Favourite *cart=(Favourite *)[NSEntityDescription insertNewObjectForEntityForName:@"Favourite" inManagedObjectContext:app.managedObjectContext];
//
// cart.favId=[[arrayMyFavourite objectAtIndex:indexPath.row]valueForKey:@"id"];
// [app saveContext];
if([[[arrayMyFavourite objectAtIndex:indexPath.row]valueForKey:@"watch_name"] isKindOfClass:[NSNull class]])
{
if([[[arrayMyFavourite objectAtIndex:indexPath.row]valueForKey:@"watch_price"] isKindOfClass:[NSNull class]])
{
cell.lblAdsTitle.text=@"NA";
cell.lblprice.text=@"NA";
}
}
else
{
//NSLog(@"%@",[[arrayMyFavourite objectAtIndex:indexPath.row]valueForKey:@"id"]);
//NSLog(@"%@",[[arrayMyFavourite objectAtIndex:indexPath.row]valueForKey:@"images_file"]);
if([[[arrayMyFavourite objectAtIndex:indexPath.row] valueForKey:@"iso"] isKindOfClass:[NSNull class]])
{
cell.imgLogo.image = [UIImage imageNamed:@"Kuwait.svg.png"];
}
else
{
[cell showImage:arrayMyFavourite :indexPath.row];
}
NSString * imageURL=[NSString stringWithFormat:@"%@%@%@/%@",BaseURL,@"uploads/watch/",[[arrayMyFavourite objectAtIndex:indexPath.row]valueForKey:@"id"],[[arrayMyFavourite objectAtIndex:indexPath.row]valueForKey:@"images_file"]];
// [cell.imgMain sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:@"wachizLogoIcon.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
// {
// }];
[cell.imgMain sd_setImageWithURL:[NSURL URLWithString:imageURL]
placeholderImage:[UIImage imageNamed:@"wachizLogoIcon.png"]
options:SDWebImageRefreshCached];
cell.lblAdsTitle.text=[[arrayMyFavourite objectAtIndex:indexPath.row]valueForKey:@"watch_name"];
cell.lblprice.text=[[arrayMyFavourite objectAtIndex:indexPath.row]valueForKey:@"watch_price"];
[cell.btnISO setTitle:@"NS" forState:UIControlStateNormal];
for (NSDictionary *dict in [[arrayMyFavourite objectAtIndex:indexPath.row]valueForKey:@"features"]) {
if ([[dict objectForKey:@"feature_name"] isKindOfClass:[NSString class]]&&[[dict objectForKey:@"feature_name"] isEqualToString:@"Currency"]) {
[cell.btnISO setTitle:[dict objectForKey:@"feature_value"] forState:UIControlStateNormal];
break;
}
}
}
if(indexPath.row%2==0)
cell.imgCellBg.backgroundColor=[UIColor whiteColor];
else
cell.imgCellBg.backgroundColor=[UIColor colorWithRed:0.92 green:0.91 blue:0.91 alpha:1.0];
}
else if (tableView==self.tableMySearch)
{
self.btnMyFavoriteDeleteCheckBox.hidden=YES;
self.btnMySearchesDeleteCheckBox.hidden=NO;
self.btnMyAdsDeleteCheckBox.hidden=YES;
self.btnMyFavoriteDeleteAction.hidden=YES;
self.btnMySearchesDeleteAction.hidden=NO;
self.btnMyAdsDeleteAction.hidden=YES;
cell.lblDeleteMySearch.text=[[arrayMySearch objectAtIndex:indexPath.row] valueForKey:@"id"];
if(checkBoxTagValue==2)
{
//cell.btnSearchDeleteAction.selected=YES;
cell.imgCheckbox.image=[UIImage imageNamed:@"markedmw.png"];
}
else
{
//cell.btnSearchDeleteAction.selected=NO;
cell.imgCheckbox.image=[UIImage imageNamed:@"unmarkedmw.png"];
}
if([[[arrayNewMySearch objectAtIndex:indexPath.row]valueForKey:@"searchKey"] isKindOfClass:[NSNull class] ] || [[[arrayNewMySearch objectAtIndex:indexPath.row]valueForKey:@"searchKey"] isEqualToString:@" "])
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
cell.lblSearchTitle.font=[UIFont fontWithName:@"Helvetica Neue Bold" size:16];
cell.lblSearchTitle.text=@"NA";
}
else
{
cell.lblSearchTitle.font=[UIFont fontWithName:@"Helvetica Neue Bold" size:14];
cell.lblSearchTitle.text=@"NA";
}
}
else
{
for(NSString * string in self.arrayDynamicSearch)
{
if([string isEqualToString:[[arrayMySearch objectAtIndex:indexPath.row] valueForKey:@"id"]])
cell.btnSearchDeleteAction.selected=YES;
}
cell.lblSearchTitle.text=[[arrayMySearch objectAtIndex:indexPath.row]valueForKey:@"favorites_name"];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
cell.lblSearchTitle.font=[UIFont fontWithName:@"Helvetica Neue Bold" size:16];
}
else
{
cell.lblSearchTitle.font=[UIFont fontWithName:@"Helvetica Neue Bold" size:14];
}
}
if(indexPath.row%2==0)
cell.imgCellBg.backgroundColor=[UIColor whiteColor];
else
cell.imgCellBg.backgroundColor=[UIColor colorWithRed:0.92 green:0.91 blue:0.91 alpha:1.0];
}
else if(tableView==self.tableMyAds)
{
self.btnMyFavoriteDeleteCheckBox.hidden=YES;
self.btnMySearchesDeleteCheckBox.hidden=YES;
self.btnMyAdsDeleteCheckBox.hidden=NO;
self.btnMyFavoriteDeleteAction.hidden=YES;
self.btnMySearchesDeleteAction.hidden=YES;
self.btnMyAdsDeleteAction.hidden=NO;
if ([[[arrayMyAds objectAtIndex:indexPath.row] valueForKey:@"seller_verification"] isEqualToString:@"Yes"]) {
cell.imgverification.image=[UIImage imageNamed:@"verifiedIcon.png"];
cell.lblverification.text=@"Verified";
}
else {
cell.imgverification.image=nil;
cell.lblverification.text=@"";
}
cell.lblDeleteMyAds=[[arrayMyAds objectAtIndex:indexPath.row]valueForKey:@"id"];
if(checkBoxTagValue==3)
{
//cell.btnAdsDeleteAction.selected=YES;
cell.imgCheckbox.image=[UIImage imageNamed:@"markedmw.png"];
}
else
{
//cell.btnAdsDeleteAction.selected=NO;
cell.imgCheckbox.image=[UIImage imageNamed:@"unmarkedmw.png"];
}
cell.btnGreen.hidden=YES;
cell.btnAdsDeleteAction.hidden=NO;
if([[[arrayMyAds objectAtIndex:indexPath.row]valueForKey:@"favorite_id"] isKindOfClass:[NSNull class]])
{
cell.lblDeleteMyFavourite.text=@"";
}
else
{
for(NSString * string in self.arrayDynamicAds)
{
if([string isEqualToString:[[arrayMyAds objectAtIndex:indexPath.row]valueForKey:@"id"]])
cell.btnAdsDeleteAction.selected=YES;
}
cell.lblDeleteMyFavourite.text=[[arrayMyAds objectAtIndex:indexPath.row]valueForKey:@"favorite_id"];
}
if([[[arrayMyAds objectAtIndex:indexPath.row]valueForKey:@"watch_name"] isKindOfClass:[NSNull class]])
{
if([[[arrayMyAds objectAtIndex:indexPath.row]valueForKey:@"watch_price"] isKindOfClass:[NSNull class]])
{
cell.lblAdsTitle.text=@"NA";
cell.lblprice.text=@"NA";
}
}
else
{
if([[[arrayMyAds objectAtIndex:indexPath.row] valueForKey:@"iso"] isKindOfClass:[NSNull class]])
{
cell.imgLogo.image = [UIImage imageNamed:@"Kuwait.svg.png"];
}
else
{
[cell showImage:arrayMyAds :indexPath.row];
}
NSString * imageURL=[NSString stringWithFormat:@"%@%@%@/%@",BaseURL,@"uploads/watch/",[[arrayMyAds objectAtIndex:indexPath.row]valueForKey:@"id"],[[arrayMyAds objectAtIndex:indexPath.row]valueForKey:@"images_file"]];
// [cell.imgMain sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:@"wachizLogoIcon.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
// {
// }];
[cell.imgMain sd_setImageWithURL:[NSURL URLWithString:imageURL]
placeholderImage:[UIImage imageNamed:@"wachizLogoIcon.png"]
options:SDWebImageRefreshCached];
cell.lblAdsTitle.text=[[arrayMyAds objectAtIndex:indexPath.row]valueForKey:@"watch_name"];
cell.lblprice.text=[[arrayMyAds objectAtIndex:indexPath.row]valueForKey:@"watch_price"];
[cell.btnISO setTitle:@"NS" forState:UIControlStateNormal];
for (NSDictionary *dict in [[arrayMyAds objectAtIndex:indexPath.row]valueForKey:@"features"]) {
if ([[dict objectForKey:@"feature_name"] isKindOfClass:[NSString class]]&&[[dict objectForKey:@"feature_name"] isEqualToString:@"Currency"]) {
[cell.btnISO setTitle:[dict objectForKey:@"feature_value"] forState:UIControlStateNormal];
break;
}
}
}
if(indexPath.row%2==0)
cell.imgCellBg.backgroundColor=[UIColor whiteColor];
else
cell.imgCellBg.backgroundColor=[UIColor colorWithRed:0.92 green:0.91 blue:0.91 alpha:1.0];
}
cell.dp=self;
return cell;
}
我可以在逻辑方面帮助您:
UITableViewCell
如您所说,每次上下滚动时都会重复使用,以节省内存
您可以做的是将所有单元格的状态保存在一个模型中
假设您的UITableView
中只有一个部分:
在一个NSMutableArray
中存储单元格的状态(选中/未选中:Bool
对象的NSNumber
),并在cellforrowatinexpath
中相对于数组中的内容更改UIButton
按下ui按钮
时,您可以更改NSMutableArray
中单元格状态的值
我希望这有帮助
编辑:
你可以找到一个有效的例子
洞的想法是你要保持“检查”信息存储在模型中,因为您不能依赖用户界面提供给您的信息,并且在任何时候显示您的单元格时,您都会使用您的模型并显示它提供的信息。查看此链接,它会帮助您@Anbu.Karthik我提到过我不希望使用UITableViewCellAccessoryCheckmark在
UITableViewCellAccessoryCheckmark中思考,在这里添加你的“勾号”按钮索引就这样,如果你不明白,只要显示你的代码我修改我的答案1。创建您自己的视图并将其添加到附件视图中。。第二种选择---播放选中和未选中状态的按钮请发布一个示例项目听起来很棒@mejdi请帮助我提供一个示例代码+从我身边谢谢。但我误读了你们samplehey@MejdiLassidi的项目文件,我按照你们的指示做了,但在我的图片中并没有改变。请帮帮我,我已经试过按钮和图片,但都没有解决