我有一个带有 CustomCells 的 UITableView。这是一个收藏夹列表,每个 CustomCell 中都有明星图像。因此,如果我再次单击点亮的星星,它们必须消失并从名为 fav.plist 的属性列表中删除。
但是当我点击点亮的星星时,它们并没有消失。我无法从 CustomCell.m 重新加载收藏列表中的数据。要消失我单击星号的数据,我必须再次查看该视图。这是重新加载数据的唯一方法。
单击星号并保持在同一视图后,如何使其重新加载数据?
这是我的 CustomCell.m 代码的一部分,其中最喜欢的列表是(列表的 UITableView 是 favView,plist 是 fav.plist):
@property (nonatomic, retain) NSMutableArray *favList;
//First I find the item from plist which one will be removed from fav.plist.
NSString *path_fav = [[self documentsDirectory] stringByAppendingPathComponent:@"fav.plist"];
self.favList = [[NSMutableArray alloc] initWithContentsOfFile:path_fav];
for (NSInteger i=0; i<[self.favList count];i++) {
d = [self.favList objectAtIndex:i];
NSString *code=[d objectForKey:@"CODE"];
NSComparisonResult res=[code compare:self.cityLabel.text];
if(res==NSOrderedSame){
//removing from the fav.plist
[self.favList removeObjectAtIndex:i];
//Here I try to reload the data for the UITableView called favView
//In Favorite.m is my UITableView called favView
NSString *path = [[self documentsDirectory]
stringByAppendingPathComponent:@"fav.plist"];
[self.favList writeToFile:path atomically:TRUE];
Favorite *favController =[[Favorite alloc]init];
[favController.favView reloadData];
[favController.favView reloadInputViews];
}
}
这是我的 Favorite.m 代码的一部分:
- (UITableViewCell*)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"cellid";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellID ];
if (cell == nil)
{
cell = (CustomCell*)[[[NSBundle mainBundle]
loadNibNamed:@"CustomCell" owner:nil options:nil]
lastObject];
}
NSDictionary *d = [self.favList objectAtIndex:indexPath.row];
cell.nameLabel.text = [d objectForKey:@"NAME"];
cell.cityLabel.text = [d objectForKey:@"CODE"];
cell.index= [NSString stringWithFormat:@"%d",indexPath.row];
cell.indexPath = indexPath;
NSString *starName;
if([[d objectForKey:@"FAV"] intValue]==0){
starName=@"star.png";
}else{
starName=@"starb.png";
}
[cell.self.starbtn setImage:[UIImage imageNamed:starName] forState:UIControlStateNormal];
if((indexPath.row % 2) ==0)
cell.contentView.backgroundColor=[UIColor colorWithRed:241/256.0 green:237/256.0 blue:237/256.0 alpha:1.0];
CGSize maxSize = CGSizeMake(320/2, MAXFLOAT);
CGSize nameSize = [cell.nameLabel.text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
if(nameSize.height>=72)
nameSize.height=63;
else
nameSize.height=38;
cell.nameLabel.frame =CGRectMake(80, 0, 213, nameSize.height);
return cell;
}