我的应用程序将帖子从互联网下载到 UITableViewCell。当用户点击单元格时,我需要将这篇文章设置为已读(如在 Mail.app 上)。我不知道该怎么做。我正在将 url(当用户按下单元格时)添加到数据库(如“url|url|url”),打开详细信息视图。当他返回 UITableView 检查数据库中现有的 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。
我如何重新加载单元格并再次调用 DRAWRECT?))