如何使用自定义单元格将复选框添加到我的 UITableView?
How to add check box to my UITableView using custom cell?
我有一个带有自定义单元格的 UITableview。在那个单元格中,我有一个按钮,我计划将其用作复选框。这意味着在单击按钮时我将图像更改为 "tick mark"。再次选择它会变为空按钮。我已经完成了那部分,但问题是因为 UITablecell 重用了单元格,如果我向下滚动,则勾号再次出现在非选定单元格中。请帮我提供示例代码或逻辑来实现它。请注意,我不想使用 UITable 默认的复选标记附件。
- (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
如您所说,每次滚动 up/down 时都会重复使用,以节省内存。
您可以做的是将所有细胞的状态保存在一个模型中。
假设您的 UITableView
中只有一个部分:
将您的细胞状态(checked/unchecked:NSNumber
个 Bool
个对象)存储在一个 NSMutableArray
中,然后在 cellForRowAtIndexPath
中您可以更改 UI相对于数组中的按钮。
当您按下 UIButton
时,您会更改 NSMutableArray
中单元格状态的值。
希望对您有所帮助。
编辑:
你找到了一个有效的例子here
这个想法是你把你的 "checked" 信息存储在一个模型中,因为你不能依赖 UI 给你的东西,任何时候你展示你的细胞你使用你的模型和你显示它提供的信息。
我有一个带有自定义单元格的 UITableview。在那个单元格中,我有一个按钮,我计划将其用作复选框。这意味着在单击按钮时我将图像更改为 "tick mark"。再次选择它会变为空按钮。我已经完成了那部分,但问题是因为 UITablecell 重用了单元格,如果我向下滚动,则勾号再次出现在非选定单元格中。请帮我提供示例代码或逻辑来实现它。请注意,我不想使用 UITable 默认的复选标记附件。
- (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
如您所说,每次滚动 up/down 时都会重复使用,以节省内存。
您可以做的是将所有细胞的状态保存在一个模型中。
假设您的 UITableView
中只有一个部分:
将您的细胞状态(checked/unchecked:NSNumber
个 Bool
个对象)存储在一个 NSMutableArray
中,然后在 cellForRowAtIndexPath
中您可以更改 UI相对于数组中的按钮。
当您按下 UIButton
时,您会更改 NSMutableArray
中单元格状态的值。
希望对您有所帮助。
编辑:
你找到了一个有效的例子here
这个想法是你把你的 "checked" 信息存储在一个模型中,因为你不能依赖 UI 给你的东西,任何时候你展示你的细胞你使用你的模型和你显示它提供的信息。