我已經用自定義的UITableViewCell類以編程方式創建了UITableViewCell,但是由於某些原因,值在UILabels中重複,即使從NSMutableArray讀取的值是正確的,並且我正在清除這些值自定義UITableViewCell類的prepareForReuse方法。停止在UITableViewCell中重複值
這就是cellForRow:AtIndexPath方法看起來像
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cellDictionary = [xmlMArray objectAtIndex:indexPath.row];
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MatchingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.itemsDictionary = cellDictionary;
cellDictionary = nil;
[cell drawCell];
return cell;
}
內。然後再這是我的自定義類是什麼樣子
#import "MatchingCell.h"
#import "ScreenSize.h"
@implementation MatchingCell
@synthesize itemsDictionary;
@synthesize nameString;
@synthesize addressString;
@synthesize codeString;
@synthesize scrollCell;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
nameString = [[UILabel alloc] init];
nameString.backgroundColor = [UIColor clearColor];
addressString = [[UILabel alloc] init];
addressString.backgroundColor = [UIColor clearColor];
codeString = [[UILabel alloc] init];
codeString.backgroundColor = [UIColor clearColor];
scrollCell = [[UIScrollView alloc] init];
scrollCell.backgroundColor = [UIColor whiteColor];
[scrollCell addSubview:nameString];
[scrollCell addSubview:addressString];
[scrollCell addSubview:codeString];
[self addSubview:scrollCell];
}
return self;
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)drawCell
{
nameString.frame = CGRectMake(15.0, 0.5, 70.0, 40.0);
nameString.text = [itemsDictionary objectForKey:@"Name"];
addressString.frame = CGRectMake(105.0, 0.5, 95.0, 40.0);
addressString.text = [itemsDictionary objectForKey:@"Address"];
codeString.frame = CGRectMake(220.0, 10.5, codeString.frame.size.width, 50.0);
codeString.text = [NSString stringWithFormat:@"ISN %@: %@",[itemsDictionary objectForKey:@"CodeA"] ,[itemsDictionary objectForKey:@"CodeB"]];
[codeString sizeToFit];
scrollCell.frame = CGRectMake(0.0, 0.0, ScreenWidth, 45.0);
[scrollCell setContentSize:(CGSizeMake((220.0 + codeString.frame.size.width)+15, 45.0))];
}
- (void)prepareForReuse
{
[super prepareForReuse];
nameString = nil;
nameString.text = nil;
addressString = nil;
addressString.text = nil;
codeString = nil;
codeString.text = nil;
itemsDictionary = nil;
[self didTransitionToState:UITableViewCellStateDefaultMask];
}
@end
所以我的問題是我怎麼停止重複的值當你將新的UITableViewCells滾動到視圖中時?
謝謝。
這工作...感謝您的幫助! – HurkNburkS
沒問題,我只是做了一些注意事項,你應該看看!祝你好運。 – Logan
awsome關於contentView將意味着UITableViewcell仍然是可選的,因爲當它覆蓋scrollview時它不再可選。 – HurkNburkS