读/未读单元格(使用drawrect)

时间:2010-12-30 11:44:56

标签: iphone sdk uitableview drawrect

我的应用程序从互联网下载帖子到UITableViewCell。当用户点击单元格时,我需要将此文章设置为已读(如Mail.app上)。我不知道该怎么做。 我将url(当用户按下单元格时)添加到数据库(如“url | url | url”),打开详细视图。当他回到UITableView检查DB中的现有url时。表现在太慢了! 你可以帮助我说出我可以使用的其他方法吗?就像在mail.app中一样。我在drawRect方法中使用自定义单元格和代码。请帮忙

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            NSString *CellIdentifier = [NSString stringWithFormat:@"%d",indexPath.row];
            NSMutableArray *array=[NSMutableArray array];
            if([[NSUserDefaults standardUserDefaults] objectForKey:@"readPostS"]!=nil)
    [array addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"readPostS"]];
            NSUInteger indexOfObject = [array count]>0?[array indexOfObject:[NSNumber numberWithInt:indexPath.row]]:100000;  //I add this, because row don't want update using updateRowAtIndexPath

            QuickCell *cell = (QuickCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil || (indexOfObject!=NSNotFound && indexOfObject!=100000))
            {
                cell = [[[QuickCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
                cell.frame = CGRectMake(0.0, 0.0, 320.0, 68.0);
                cell.accessoryType = UITableViewCellAccessoryNone;
                if([self.subject count]>0)
            {
                [self.allImages addObject:[NSNull null]];
                NSString *userNameLJ = [NSString stringWithFormat:@"%@",[self.journalurl objectAtIndex:indexPath.row]];
                userNameLJ = [userNameLJ stringByReplacingOccurrencesOfString:@"http://"
                                                                   withString:@""];
                userNameLJ = [userNameLJ stringByReplacingOccurrencesOfString:@".livejournal.com"
                                                                   withString:@""];
                userNameLJ = [userNameLJ stringByReplacingOccurrencesOfString:@"community/"
                                                                   withString:@""];
                NSString *postURL = [NSString stringWithFormat:@"http://m.livejournal.com/read/user/%@/%@",userNameLJ,[self.ditemid objectAtIndex:indexPath.row]];


                NSMutableDictionary *dict = [NSMutableDictionary dictionary];
                [dict setObject:[self.subject objectAtIndex:indexPath.row] forKey:@"title"];
                [dict setObject:[Utilities replaceForCell:[Utilities flattenHTML:[self.entry objectAtIndex:indexPath.row]]] forKey:@"description"];

                if([[database executeQuery:@"SELECT * FROM read WHERE posts=?;",postURL] count]>0)
            {
                //if this post is read
                [dict setObject:[NSNumber numberWithBool:NO] forKey:@"highlighted"];
            }   
                else {
                    [dict setObject:[NSNumber numberWithBool:YES] forKey:@"highlighted"];
                }


            [dict setObject:[self.journalname objectAtIndex:indexPath.row] forKey:@"nick"];

            [cell setContentForCell:dict];

                BOOL trueOrFalse = [[self.allImages objectAtIndex:indexPath.row] isKindOfClass:[NSNull class]]?YES:NO;
            if(trueOrFalse)
        {
            if (tableView.dragging == NO && tableView.decelerating == NO)
            {
                    //if no image cached now, download it
                if(indexPath.row<6 && self.updating==YES)
                    [self startDownloading:indexPath];
                if(self.updating==NO)
                    [self startDownloading:indexPath];
            }

            }
            else
            {
                [cell setImageForCell:[self.allImages objectAtIndex:indexPath.row]];
        }

            }

                if(indexOfObject!=NSNotFound && indexOfObject!=100000)
                {
                       //delete this row from userdefaults
                    [array removeObjectAtIndex:indexOfObject];
                    if([array count]>0)
                        [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"readPostS"];
                    else [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"readPostS"];

                }
            }


            return cell;
        }

//////////////////////////////////////////////////////////////
///////-(void)viewWillApear
//////////////////////////////////////////////////////////////
    NSMutableArray *readPosts = [NSMutableArray array];
                if([[NSUserDefaults standardUserDefaults] objectForKey:@"readPostS"]!=nil)
                {
                    [readPosts addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"readPostS"]];
                    self.read = (NSMutableArray*)[database executeQuery:@"SELECT * FROM read;"];
                    for(int i=0;i<[readPosts count];i++)
                    {

                    [self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[[readPosts objectAtIndex:i] intValue] inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
                    }
                }

///////////////////////////////////////////////
///////THIS FILE OPENS POSTS FOR READING
///////////////////////////////////////////////
if([[database executeQuery:@"SELECT * FROM read WHERE posts=?;",self.postURL] count]==0)
    {
        [database executeNonQuery:@"INSERT INTO read VALUES (?);",self.postURL];

        NSMutableArray *defaults = [NSMutableArray array];
        if([[NSUserDefaults standardUserDefaults] objectForKey:@"readPostS"]!=nil)
            defaults = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"readPostS"]];
        [defaults addObject:[NSNumber numberWithInt:self.currentIndex]];
        [[NSUserDefaults standardUserDefaults] setObject:defaults forKey:@"readPostS"];
    }

确定。我想,我的代码很糟糕,你可以理解,我在这里做什么)回答其他问题。我如何更新隐藏的细胞?用户片刻只看到6个单元格,我需要更新,例如10个单元格。怎么样? 或者,如果已经存在,如何重新加载单元格?让我们说 -

NSString *CellIdentifier = [NSString stringWithFormat:@"%d",indexPath.row];
QuickCell *cell = (QuickCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[QuickCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.frame = CGRectMake(0.0, 0.0, 320.0, 68.0);
        cell.accessoryType = UITableViewCellAccessoryNone;


.........}

现在单元格存在,如果我调用重载数据没有任何反应。并且reloadCellAtIndexPath也不起作用,因为单元格具有唯一标识符和ixists。

我如何重新安装电池并再次拨打电话?))

2 个答案:

答案 0 :(得分:0)

您可以通过在

中添加代码来实现此功能
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

//Add the indexPath.row values in an array.
}

添加自定义功能(如在

中显示BOLD中的标题为未读)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

//Check if the indexPath.row is available in the array
//If yes then Normalize the fonts
//Else Keep them BOLD.

}

答案 1 :(得分:0)

除非你真的在做一些绘画,否则不要在drawRect:中实施。我不知道你是具体的代码,所以在你的情况下可能不正确。

要重新加载整个表格(所有单元格),请查看UITableView的- (void)reloadData

要重新加载特定单元格,请查看UITableView的- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation并使用它重新加载相关单元格。