我已经实现了ABTableViewCell,因为我的iPhone应用程序很难用自定义单元格滚动桌面视图。
一切都很好,花花公子,除了一件事,我无法弄清楚如何解决它。 UITableView从解析的XML元素数组中获取数据,然后我的RootViewController使用cellForRowAtIndexPath
将此数据提供给CustomCell。我正确地绘制了UITableView的每个CustomCell,当我向下滚动时,我看到重复的条目,只有在选中时才会更改为正确的单元格标题。
因此,在15个表条目中,只有9或10个具有正确的数据,当绘制视图时,5或6个视图之外仅在选择时显示正确的数据。任何人都可以告诉我我做错了什么或我在这里缺少什么?谢谢!
RootViewController.h:
#import <UIKit/UIKit.h>
@interface RootViewController : UITableViewController {
MyAppDelegate *appDelegate;
UIToolbar *toolbar;
NSArray *tableDataArray;
}
@property (nonatomic, retain) NSArray *tableDataArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil tableDataSource:(NSArray*)tableData;
@end
RootViewController.m:
#import "CustomCell.h"
#import "MyAppDelegate.h"
#import "RootViewController.h"
#import "DetailViewController.h"
@implementation RootViewController
@synthesize tableDataArray;
- (void)viewDidLoad {
[super viewDidLoad];
}
//Override the default initWithNibName method
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil tableDataSource:(NSArray*)tableData {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
tableDataArray = [tableData retain];
}
return self;
}
-(void)viewWillAppear:(BOOL)animated {
appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[super viewWillAppear:animated];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.tableDataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if(cell == nil) {
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CustomCellIdentifier] retain];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSUInteger row = [indexPath row];
NSDictionary *rowData = [self.tableDataArray objectAtIndex:row];
cell.cellSubText = [rowData objectForKey:@"Date"];
cell.cellText = [rowData objectForKey:@"Name"];
cell.cellImage = [rowData objectForKey:@"Image"];
cell.cellId = [rowData objectForKey:@"Id"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedCell = [tableDataArray objectAtIndex:indexPath.row];
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
detailViewController.selectedCell = selectedCell;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
detailViewController = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
CustomCell.h:
#import "ABTableViewCell.h"
@interface CustomCell : ABTableViewCell {
NSString * cellSubText;
NSString * cellText;
NSString * cellImage;
NSString * cellIcon;
NSString * cellId;
}
@property (nonatomic, copy) NSString *cellSubText;
@property (nonatomic, copy) NSString *cellText;
@property (nonatomic, copy) NSString *cellImage;
@property (nonatomic, copy) NSString *cellIcon;
@property (nonatomic, copy) NSString *cellId;
@end
CustomCell.m:
#import "CustomCell.h"
@implementation CustomCell
@synthesize cellSubText, cellText, cellImage, cellIcon, cellId;
static UIFont *celSubFont = nil;
static UIFont *cellFont = nil;
+ (void)initialize {
if(self == [CustomCell class]) {
cellSubFont = [[UIFont systemFontOfSize:13] retain];
cellFont = [[UIFont boldSystemFontOfSize:17] retain];
}
}
- (void)setFirstText:(NSString *)s {
[cellSubText release];
cellSubText = [s copy];
[self setNeedsDisplay];
}
- (void)setLastText:(NSString *)s {
[cellText release];
cellText = [s copy];
[self setNeedsDisplay];
}
- (void)drawContentView:(CGRect)r {
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *backgroundColour = [UIColor whiteColor];
UIColor *categoryColour = [UIColor grayColor];
UIColor *titleColour = [UIColor blackColor];
if(self.highlighted || self.selected) {
backgroundColour = [UIColor clearColor];
categoryColour = [UIColor whiteColor];
titleColour = [UIColor whiteColor];
}
[backgroundColour set];
CGContextFillRect(context, r);
CGPoint pCategory;
pCategory.x = 60;
pCategory.y = 3;
[categoryColour set];
[cellSubText drawAtPoint:pCategory forWidth:235 withFont:categoryFont minFontSize:13 actualFontSize:NULL lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentNone];
CGPoint pTitle;
pTitle.x = 60;
pTitle.y = 17;
[titleColour set];
[cellText drawAtPoint:pTitle forWidth:235 withFont:titleFont minFontSize:17 actualFontSize:NULL lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentNone];
//Display the image
CGPoint pImage;
pImage.x = 5;
pImage.y = 5;
NSData *imageData = [[NSData dataWithContentsOfURL:[NSURL URLWithString:cellImage]] retain];
UIImage *image = [UIImage imageWithData:imageData];
[image drawAtPoint:pImage];
}
- (void)dealloc {
[cellSubText release];
[cellText release];
[cellIcon release];
[cellImage release];
[super dealloc];
}
@end
更新 我想我找到了问题的根源。在旧情况下,使用慢速绘制过程,所有单元格都被提前淹没并具有索引。现在,只绘制了视图中的单元格(前八个),并且当它们进入视图时会动态创建新单元格(并获得相应的索引编号)。
我不确定如何从这里开始。有没有办法在创建数据后立即将数据提供给新单元格?
答案 0 :(得分:1)
立即跳出来的唯一一件事就是以下几行:
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CustomCellIdentifier] retain];
你在这里创建了一个内存泄漏,这可能解释了为什么旧的单元格内容仍然存在。
尝试将其更改为:
cell = [[[CustomCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier]autorelease];
答案 1 :(得分:0)
问题不在于如何下载图像。您需要为加载新单元格时需要更改的变量调用setNeedsDisplay。
- (void)setCellImage:(NSString *)s
{
cellImage = [s copy];
[self setNeedsDisplay];
}
否则,新变量不会更新。
希望这有帮助。