我有一个tableView,它在每一行中保存正在运行的计时器的输出。它工作正常,直到我尝试在视图的末尾添加一个单元格,然后显示的最后一个单元格与新计时器重叠。我正在使用可重复使用的单元格,我非常怀疑存在内存问题。如果有人有建议,我将不胜感激。我不会发布整个代码,因为它非常混乱(我是一个新开发人员),但这里是与单元格有关的代码部分。
- (void)tableView: (UITableView*)tableView
willDisplayCell: (UITableViewCell*)cell
forRowAtIndexPath: (NSIndexPath*)indexPath
{
cell.backgroundColor = indexPath.row % 2
? [UIColor colorWithRed: 0.0 green: 0.3 blue: 1.0 alpha: 0.3]
: [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}
// *********************************************************************
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[NRCItemStore sharedStore] allItems] count];
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 60.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Get a new or recycled cell
NRCItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NRCItemCell" forIndexPath:indexPath];
self.timerItem.cell = cell;
[self handleTimer];
return cell;
//
// NRCItemCell.h
// timeLogger
//
// Created by Nelson Capes on 10/8/15.
// Copyright © 2015 Nelson Capes. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface NRCItemCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *timerName;
@property (weak, nonatomic) IBOutlet UILabel *timer;
@property (weak, nonatomic) IBOutlet UIImageView *thumbnailView;
@end
//
// NRCItemStore.m
// timeLogger
#import "NRCItemStore.h"
#import "NRCtimerItem.h"
#import "NRCImageStore.h"
@interface NRCItemStore ()
@property (nonatomic) NSMutableArray *privateItems;
@end
@implementation NRCItemStore
+ (instancetype)sharedStore
{
static NRCItemStore *sharedStore;
// Do I need to create a sharedStore?
if (!sharedStore) {
sharedStore = [[self alloc] initPrivate];
}
return sharedStore;
}
// If a programmer calls [[NRCItemStore alloc] init], let him
// know the error of his ways
- (instancetype)init
{
@throw [NSException exceptionWithName:@"Singleton"
reason:@"Use +[NRCItemStore sharedStore]"
userInfo:nil];
return nil;
}
// Here is the real (secret) initializer
- (instancetype)initPrivate
{
self = [super init];
if (self) {
NSString *path = [self itemArchivePath];
_privateItems = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
// If the array hadn't been saved previously, create a new empty one
if (!_privateItems) {
_privateItems = [[NSMutableArray alloc] init];
}
}
return self;
}
- (NSString *)itemArchivePath
{
// Make sure that the first argument is NSDocumentDirectory
// and not NSDocumentationDirectory
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// Get the one document directory from that list
NSString *documentDirectory = [documentDirectories firstObject];
return [documentDirectory stringByAppendingPathComponent:@"items.archive"];
}
- (BOOL)saveChanges
{
NSString *path = [self itemArchivePath];
// Returns YES on success
return [NSKeyedArchiver archiveRootObject:self.privateItems toFile:path];
}
- (NSArray *)allItems
{
return [self.privateItems copy];
}
- (NRCtimerItem *)createItem
{
NRCtimerItem *item = [[NRCtimerItem alloc] init];
[self.privateItems addObject:item];
return item;
}
- (void)removeItem:(NRCtimerItem *)item
{
[self.privateItems removeObjectIdenticalTo:item];
}
- (void)moveItemAtIndex:(NSInteger)fromIndex
toIndex:(NSInteger)toIndex
{
if (fromIndex == toIndex) {
return;
}
// Get pointer to object being moved so you can re-insert it
NRCtimerItem *item = self.privateItems[fromIndex];
// Remove item from array
[self.privateItems removeObjectAtIndex:fromIndex];
// Insert item in array at new location
[self.privateItems insertObject:item atIndex:toIndex];
}
@end