UITableView无法从CustomCell重新加载数据

时间:2013-08-28 06:42:50

标签: objective-c uitableview plist reloaddata

我有一个带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;

}

2 个答案:

答案 0 :(得分:0)

您是否实施了方法

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:

如果是,则在此执行删除并重新加载此方法。

答案 1 :(得分:0)

您无法从自身重新加载单元格。就我的问题而言,您需要为您的单元格实现委托协议。当按下星号时,在customcell.m中调用按钮的方法,从plist文件中删除星号并执行其他操作。然后在委托(您的视图)上调用方法来重新加载该单元格。如果你不明白我可以提供你的例子。