我有自定义单元格的UITableview。在那个单元格中,我有一个按钮,我打算将其用作复选框。这意味着在单击按钮时我将图像更改为"刻度线"。再次选择它会更改为空按钮。我已经完成了那一部分,但问题是因为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;
}
答案 0 :(得分:1)
我可以用逻辑帮助你:
正如你所说的那样 UITableViewCell
每次向上/向下滚动以便节省内存时都会重复使用。
您可以做的是将所有单元格的状态保存在一个模型中。
我们假设您的UITableView
只有一个部分:
在NSMutableArray
个单元格中存储NSNumber
个状态(已选中/取消选中:Bool
个cellForRowAtIndexPath
个对象),并在UIButton
中相对于您更改UIButton在你的阵列中。
按下NSMutableArray
后,您可以更改try{
buffer.readManyAsync(sequence, 1, 3, null).get(500, TimeUnit.MILLISECONDS);
} catch (TimeoutException e){
// We timed out, shame, let's move on
}
中单元格状态的值。
我希望这会有所帮助。
修改强>
你找到了一个有效的例子here
洞的想法是你保持"检查"存储在模型中的信息,因为您无法依赖UI提供给您的任何时候,只要您显示您的单元格,您就可以使用模型并显示它所提供的信息。