2012-09-28 16 views
0

我做了重大更改併爲TableView單元格設置了自定義類別。不理解重用表格視圖單元格

Cell.h

#import <UIKit/UIKit.h> 

@interface Cell : UITableViewCell 
@property (nonatomic, retain) UILabel *month; 
@property (nonatomic, retain) UILabel *date; 
@end 

Cell.m

#import "Cell.h" 

@implementation Cell 
@synthesize date, month; 
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 
- (void)layoutSubviews { 
    [super layoutSubviews]; 
    self.imageView.frame = CGRectMake(1,1,69,69); 

    float limgW = self.imageView.image.size.width; 
    if(limgW > 0) { 
     UIFont *cellFont3 = [UIFont fontWithName:@"ArialRoundedMTBold" size:9]; 
     UIFont *cellFont4 = [UIFont fontWithName:@"ArialRoundedMTBold" size:18]; 
     self.textLabel.frame = CGRectMake(82,0,228,53); 
     self.detailTextLabel.frame = CGRectMake(82, 20, 228, 53); 
     self.month = [[UILabel alloc] initWithFrame:CGRectMake(8, 10, 53, 21)]; 
     self.month.font = cellFont3; 
     self.month.textAlignment = UITextAlignmentCenter; 
     self.month.backgroundColor = [UIColor clearColor]; 

     self.date = [[UILabel alloc] initWithFrame:CGRectMake(11, 21, 50, 45)]; 
     self.date.backgroundColor = [UIColor clearColor]; 
     self.date.font = cellFont4; 
     self.date.textAlignment = UITextAlignmentCenter; 

    } 
} 
@end 

然後在我的TableView代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[Cell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 

    } 


    RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row]; 

    NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; 
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 
    [dateFormatter setDateStyle:NSDateFormatterShortStyle]; 

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"MM"]; 
    NSString *monthfromdate = [formatter stringFromDate:entry.articleDate]; 
    NSLog(@"%@", monthfromdate); 
    [formatter release]; 

    NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init]; 
    [formatter2 setDateFormat:@"dd"]; 
    NSString *datefromdate = [formatter2 stringFromDate:entry.articleDate]; 
    NSLog(@"%@", datefromdate); 
    [formatter2 release]; 

    if ([monthfromdate isEqualToString:@"01"]) { 
     self.currentmonth = @"January"; 
    } 
    if ([monthfromdate isEqualToString:@"02"]) { 
     self.currentmonth = @"February"; 
    } 
    if ([monthfromdate isEqualToString:@"03"]) { 
     self.currentmonth = @"March"; 
    } 
    if ([monthfromdate isEqualToString:@"04"]) { 
     self.currentmonth = @"April"; 
    } 
    if ([monthfromdate isEqualToString:@"05"]) { 
     self.currentmonth = @"May"; 
    } 
    if ([monthfromdate isEqualToString:@"06"]) { 
     self.currentmonth = @"June"; 
    } 
    if ([monthfromdate isEqualToString:@"07"]) { 
     self.currentmonth = @"July"; 
    } 
    if ([monthfromdate isEqualToString:@"08"]) { 
     self.currentmonth = @"August"; 
    } 
    if ([monthfromdate isEqualToString:@"09"]) { 
     self.currentmonth = @"September"; 
    } 
    if ([monthfromdate isEqualToString:@"10"]) { 
     self.currentmonth = @"October"; 
    } 
    if ([monthfromdate isEqualToString:@"11"]) { 
     self.currentmonth = @"November"; 
    } 
    if ([monthfromdate isEqualToString:@"12"]) { 
     self.currentmonth = @"December"; 
    } 


    NSString *currentday = datefromdate; 


    NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate]; 
    UIFont *cellFont = [UIFont fontWithName:@"ArialRoundedMTBold" size:15]; 
    UIFont *cellFont2 = [UIFont fontWithName:@"ArialRoundedMTBold" size:12]; 

    cell.imageView.image = [UIImage imageNamed:@"calendar1.png"]; 
    cell.imageView.contentMode = UIViewContentModeScaleAspectFit; 



    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", articleDateString, entry.blogTitle]; 
    cell.detailTextLabel.textColor = [UIColor grayColor]; 
    cell.detailTextLabel.font = cellFont2; 

    cell.textLabel.text = entry.articleTitle; 
    cell.textLabel.font = cellFont; 
    cell.month.text = currentmonth; 
    cell.date.text = currentday; 
    return cell; 
} 

這解決了重複的問題,或標籤被寫在上面彼此,但只有cell.textLabel顯示任何東西。 detailTextLabel或我設置的其他2個標籤上沒有顯示任何內容。

+0

我要做的第一件事就是檢查你的數據源。這可能是由於您解析提要和創建數據模型時發生的錯誤。換句話說,查看條目數組並確保它實際上沒有那些重複條目。如果確實如此,那麼你的tablview代碼是正確的,問題可能出現在你的解析器中。 – bennythemink

+0

@bennythemink數據源正確。 – user717452

+0

好吧,快速的問題,然後澄清問題,是10月14日在您的數據源中的最後一個條目的入口,還是在您的數據源中有三個隱藏/未顯示在第29,30和03單元格中的三個條目? – bennythemink

回答

0

您的方法不正確。當單元格被重新使用時,它不一定意味着索引路徑{0,1}上的單元格將在下次該特定索引路徑單元格變爲可見時重新使用。將使用具有相同標識符的單元格,並且它可能包含不同索引路徑上不同單元格的數據。您的方法應該是取出或初始化單元,具體取決於離散後的單元存在,但始終根據索引路徑在單元上分配數據。

建議幾句話:不要在cellForRowAtIndexPath方法內創建/釋放NSDateFormatter對象。它會給你性能問題。此外,爲了組織目的,請將UITableViewCell分類並正確創建界面。在飛行中添加它們是混亂和混亂的。你也許想看看NSDateFormatter的monthSymbols和weekdaySymbols方法,以及NSDateComponent documentation。你可以得到月份和日期信息,而不是有12個「ifs」來獲得月份。

+0

我澄清了實際問題,並添加了一些顯示它的圖像。 – user717452

+0

查看我剛剛編輯的編輯。解決了不加載所有內容的問題,現在無法加載所有的TextLabel – user717452

0

將此移至答案,因爲我認爲這可能是解決方案(希望)。

我認爲你的標籤顯示不正確,因爲你有他們在佈局子視圖方法。當您執行類似setFrame,addSubview或調整視圖的操作時,會自動調用此方法。你沒有在你的cellForRowAtIndexPath中進行任何調用,所以操作系統不會自動進行佈局子視圖調用(我希望這是有道理的,是正確的)。如果我是你,我將爲表格單元格提供一個單獨的XIB,它更容易管理。

相關問題