2014-03-13 34 views
0

我已經用自定義的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滾動到視圖中時?

謝謝。

回答

2

採取一切初始化,並將其移動到drawCell:

你清楚你所有的引用在準備重用,但正因爲如此,他們永遠都不會重建的:

if (cell == nil) { 
    cell = [[MatchingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

所以,你的init只有第一次運行。

該單元格仍然存在,但它的子視圖沒有任何引用。

札記二

你應該從子視圖中刪除,以及在備再用,你無了引用之前。因爲否則你會繼續在子視圖上添加子視圖,你會得到不好的表現。 (如@ user2891327所示)

您不應將視圖直接添加到單元格,請將其.contentView屬性用於子視圖。

+0

這工作...感謝您的幫助! – HurkNburkS

+0

沒問題,我只是做了一些注意事項,你應該看看!祝你好運。 – Logan

+0

awsome關於contentView將意味着UITableViewcell仍然是可選的,因爲當它覆蓋scrollview時它不再可選。 – HurkNburkS

0

出隊

for (UIView* view in cell.subviews) 
    [view removeFromSuperview]; 

後試試這個。如果它的工作原理,這意味着你需要先刪除子視圖。

+0

對不起,我在哪裏試試?..好吧現在就試試吧。 – HurkNburkS

+0

好吧我補充說,它將滾動出視圖後刪除所有的UITableViewCells。 – HurkNburkS

+0

單元= [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; –