Recreating the functionality of the blue “unread dot” on the iPhone Mail app
I am building an RSS reader with a GUI very similar to the built-in mail app. It uses Core Data to store the information once it is downloaded. When a story is downloaded, it has a blue dot to indicate it is new. Once I go back to the main page after reading a story the dot should be gone. It stays there until I scroll or relaunch the app. In the viewWillAppear:
method, I call [self.tableView reloadData];
which successfully calls cellForRowAtIndexPath:
for all visible cells. Here is my cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"StoryCellIdentifier";
StoryCell *cell = (StoryCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[NSBundle mainBundle] loadNibNamed:@"StoryCell" owner:self options:nil] objectAtIndex:0];
}
NSUInteger thisRow = [indexPath row];
NSManagedObject *managedObject = [storyData objectAtIndex:thisRow];
cell.titleLabel.text = [[managedObject valueForKey:@"title"] description];
cell.descLabel.text = [[managedObject valueForKey:@"subTitle"] description];
if (!([managedObject valueForKey:@"new"]))
{
cell.readIndicator.image = nil;
}
return cell;
}
The program hits the cell.readIndicator.image = nil;
line when it should. In fact, the program follows the same execution path both when the dot is and is not there. Also, this is probably related, but when I click back on the Navigation Controller, the cell I had clicked on is still highlighted.
EDIT: The .m file that corresponds to the .xib is just boilerplate.
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier])
{ }
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
EDIT2:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
StoryView *storyView = [[StoryView alloc] initWithNibName:@"StoryView" bundle:nil];
NewsItem *item = [storyData objectAtIndex:[indexPath row]];
[storyView viewLoaded:item];
// Pass the selected object to the new view controller.
// ...
item.new = NO;
[managedObjectContext save:nil];
[self.navigationController push开发者_运维问答ViewController:storyView animated:YES];
[storyView release];
}
One thing that is probably a separate bug is that your cell will show up wrong if a cell with an already-read item is re-used to display a not-read item... you need to do something like
if (!([managedObject valueForKey:@"new"]))
{
cell.readIndicator.image = nil;
}
else
{
cell.readIndicator.image = blueDotImage;
}
rather than just assuming that the blue dot image was put there when the cell was created.
For the non-displaying part, I'm wondering if you need to call setNeedsDisplay
--perhaps the cell isn't realizing it needs to be redrawn when you change the readIndicator
's image.
For the cell remaining highlighted when popping VC, see this
For the main issue, perhaps you should post relevant parts of the custom table cell code for us to look at.
Is your viewWillAppear
method firing? Try adding the [table reloadData]
statement to the didSelectRowAtIndexPath
method and see if that is working.
I had issues with the viewWillAppear
method not always being called. Checkout this thread for more info.
精彩评论