显示tableview单元格是否被点击过



我正在开发一个带有新闻提要的应用程序。

这是一个PFQueryTableView从parse.com加载内容。

我也有一个细节ViewController附带一个segue。

我的问题是:

我如何显示有一个单元格以前没有点击过?

就像IOS中的邮件应用程序一样。

图片:

https://i.stack.imgur.com/vO2N3.png

- (id)initWithCoder:(NSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if (self) {
    // Custom the table
    // The className to query on
    self.parseClassName = @"Nyhet";
    // The key of the PFObject to display in the label of the default cell style
    self.textKey = @"Rubrik";
    // Whether the built-in pull-to-refresh is enabled
    self.pullToRefreshEnabled = YES;
    // Whether the built-in pagination is enabled
    self.paginationEnabled = YES;
    // The number of objects to show per page
    self.objectsPerPage = 10;
}
return self;
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleDefault;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Change button color
sidebarButton.tintColor = [UIColor whiteColor];
// Set the side bar button action. When it's tapped, it'll show up the sidebar.
sidebarButton.target = self.revealViewController;
sidebarButton.action = @selector(revealToggle:);

// Set the gesture
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (PFQuery *)queryForTable
{
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
// If no objects are loaded in memory, we look to the cache first to fill the table
// and then subsequently do a query against the network.
/*    if ([self.objects count] == 0) {
    query.cachePolicy = kPFCachePolicyCacheThenNetwork;
}*/
[query orderByDescending:@"Prioritering"];
return query;
}

// Override to customize the look of a cell representing an object. The default is to display
// a UITableViewCellStyleDefault style cell with the label being the first key in the object.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *simpleTableIdentifier = @"RecipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
UILabel *nameLabel = (UILabel*) [cell viewWithTag:101];
nameLabel.text = [object objectForKey:@"Rubrik"];
UILabel *prepTimeLabel = (UILabel*) [cell viewWithTag:102];
prepTimeLabel.text = [object objectForKey:@"Detaljer"];

return cell;
}
- (void) objectsDidLoad:(NSError *)error
{
[super objectsDidLoad:error];
NSLog(@"error: %@", [error localizedDescription]);
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    RecipeDetailViewController *destViewController = segue.destinationViewController;
    PFObject *object = [self.objects objectAtIndex:indexPath.row];
    Recipe *recipe = [[Recipe alloc] init];
    recipe.name = [object objectForKey:@"Rubrik"];
    recipe.prepTime = [object objectForKey:@"Text"];
    recipe.detaljer = [object objectForKey:@"Detaljer"];
    recipe.rubriker = [object objectForKey:@"Rubrik"];
    destViewController.recipe = recipe;
}
}

@end

我认为最好使用自定义UITableViewCell示例:

       CustomTableViewCell.h  之前
@interface CustomTableViewCell : UITableViewCell
@property(assign) bool beforeClicked
@end

 CustomTableViewCell.m  之前
#import CustomTableViewCell.h
@implementation CustomTableViewCell
@synthesize beforeClicked;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}
@end

,例子:

CustomTableViewCell.h//添加标题
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
    static NSString *simpleTableIdentifier = @"RecipeCell";
    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {
         cell = (CustomTableViewCell *)[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    UILabel *nameLabel = (UILabel*) [cell viewWithTag:101];
    nameLabel.text = [object objectForKey:@"Rubrik"];
    UILabel *prepTimeLabel = (UILabel*) [cell viewWithTag:102];
    prepTimeLabel.text = [object objectForKey:@"Detaljer"];
    cell.beforeClicked = NO;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    UITableViewCell *myCell = [tableView cellForRowAtIndexPath:indexPath];
    myCell.beforeClicked = YES;
}

我想到了两个解决方案。

  • 更简单的解决方案是使用NSMutableArray来保存所有访问的细胞。在didSelectRowAtIndexPath:中这样做:

    if (![visitedCellsArray containsObject:[NSNumber numberWithInt:indexPath.row]])
        [visitedCellsArray addObject:[NSNumber numberWithInt:indexPath.row]];
    

然后将backgroundColor设置为您的单元格(只是一个建议,您可以使用任何UI指示器-点,颜色等)在您的cellForRowAtIndexPath:

  • 第二,可能不利于你的目的。我真的不知道你正在使用的字典是否是可变的,也不知道添加键是否会对你的控制流产生任何影响。但是,如果可以添加键,则添加一个类似@"seen"的键,并在didSelectRowAtIndexPath:中使用[NSNumber numberWithBool:YES]设置它,并在cellForRowAtIndexPath:中根据该键的值添加适当的UI指示。这种方法在内存方面更好。

我会给你的PFReceipt对象一个属性BOOL new。

然后获取并设置该属性在cellForRow中你可以根据属性值设置字体

干净甚至持久。

所以重申一下:

  1. 修改PFReceipt对象,使其具有新的属性
  2. modify - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object

    ...
    cell.clickedBefore = !receipe.new; 
    
  3. 显示收据时标记为不再是新的

    ...
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
       if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
           ...
           PFObject *object = [self.objects objectAtIndex:indexPath.row]; 
           selectedCell.clickedBefore = !object.new;
    

提供的解决方案都没有考虑到应用程序可能有多个用户。如果你在对象中存储了"read"或"new"属性,这只会显示是否有人读取了它。所以如果Jane读了,Mark就会认为它是已读的。

要为所有用户单独处理这个问题,您需要另一个解析类来处理读取状态。这实现起来有点复杂,但这是在服务器上解决所有问题的唯一方法。

另一个选择是在每个客户端上存储一个读状态列表。这意味着某种本地持久存储,它知道读取了哪些食谱,并将其与从Parse获取的数据进行比较。

最新更新