带有CustomViewCell的 UITableView呈现 本地数组数据。 CustomViewCell 具有从SELECTED 更改为 UNSELECTED的“收藏夹”按钮。
问题是在向下和向上滚动时,TableView再次使用原始按钮的状态重新加载单元格。
它如何在不重新加载的情况下保持单元格?有没有像java“适配器/视图持有者”这样的方法?
谢谢你们。
主要课程:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CommentCell *cellView;
// if(cellView == nil)
// {
// _article = nil;
// cellView.lblAdress = nil;
// cellView.lblName = nil;
// cellView.webImageV = nil;
// cellView.strPhone = nil;
// // cell.btn = nil;
// }
if (!cellView) {
NSArray *topLevelObjects;
if([[[arrResultsALL objectAtIndex:indexPath.row] valueForKey:@"IsMain"]isEqualToString:@"false"])
{
topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CommentCell" owner:self options:nil];
}
if([[[arrResultsALL objectAtIndex:indexPath.row] valueForKey:@"IsMain"]isEqualToString:@"true"])
{
topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CommentCellBIG" owner:self options:nil];
}
cellView = [topLevelObjects objectAtIndex:0];
}
// if ([[[arrResultsALL objectAtIndex:indexPath.row] valueForKey:@"AlreadyFavorite"]isEqualToString:@"true"]) {
//
// [cellView.btn setSelected:YES];
// }
// else if ([[[arrResultsALL objectAtIndex:indexPath.row] valueForKey:@"AlreadyFavorite"]isEqualToString:@"false"]) {
// [cellView.btn setSelected:NO];
// }
_article = [articles objectAtIndex:[indexPath row]];
[cellView setArticle:_article];
return cellView;
}
加载单元格数据:
-(void)loadArticle
{
[articles removeAllObjects];
for (int i =0; i<[arrResultsALL count]; i++)
{
_article = [[Comment alloc]initWithName:[[arrResultsALL objectAtIndex:i] valueForKey:@"name"]
Adress:[[arrResultsALL objectAtIndex:i] valueForKey:@"address"]
ImageURL:[[arrResultsALL objectAtIndex:i] valueForKey:@"logo"]
ObjID:[[arrResultsALL objectAtIndex:i] valueForKey:@"id"]
Phone:[[arrResultsALL objectAtIndex:i] valueForKey:@"phone"]
Favorites:[[arrResultsALL objectAtIndex:i] valueForKey:@"AlreadyFavorite"]
IsMain:[[arrResultsALL objectAtIndex:i] valueForKey:@"IsMain"]
];
[articles addObject:_article];
}
}
**
自定义单元类:
**
-(void)setArticle:(Comment*)_article {
self.webImageV.image = [UIImage imageNamed:@"AdPictureFrame"];
if (!StringIsNilOrEmpty(_article.ImageURL)) {
NSURL *animationUrl = [NSURL URLWithString:_article.ImageURL];
if (!StringIsNilOrEmpty(animationUrl.pathExtension)) {
self.webImageV.image = nil;
[self.webImageV loadFromURL:animationUrl];
}
}
self.strAlreadyFavorites=_article.AlreadyFavorites;
self.strIsMain = _article.IsMain;
NSLog(@"strAlreadyFavoritesstrAlreadyFavorites=%@",strAlreadyFavorites);
if ([strAlreadyFavorites isEqualToString:@"true"]) {
[self.btn setSelected:YES];
}
else
{
[self.btn setSelected:NO];
}
self.ObjectID =_article.ObjID;
self.lblName.text =_article.Name;
self.lblAdress.text =_article.Adress;
strPhone =_article.Phone;
}