2012-09-21 67 views
0

我的tableview分析日历RSS源。我将单元格设置为每个单元格都有一个空白日历图标作为图像,并添加使用来自RSS本身的NSDate检测的月份和日期的子视图。但是,由于某种原因,它显示每个事件2个单元格,我无法弄清楚原因。下面是该小区的代码:UITableView显示每个项目的2个单元格

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

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    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]; 
    UIFont *cellFont3 = [UIFont fontWithName:@"ArialRoundedMTBold" size:9]; 
    UIFont *cellFont4 = [UIFont fontWithName:@"ArialRoundedMTBold" size:18]; 

    UILabel *month = [[UILabel alloc] initWithFrame:CGRectMake(8, 10, 53, 21)]; 
    month.font = cellFont3; 
    month.text = currentmonth; 
    month.textAlignment = UITextAlignmentCenter; 
    month.backgroundColor = [UIColor clearColor]; 

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

    UIImageView *alternate = [[UIImageView alloc] initWithFrame:CGRectMake(1,1,69,69)]; 
    alternate.image = [UIImage imageNamed:@"calendar1.png"]; 
    alternate.contentMode = UIViewContentModeScaleAspectFit; 
    UILabel *alternatelabel = [[UILabel alloc] initWithFrame:CGRectMake(82,0,228,53)]; 

    alternatelabel.backgroundColor = [UIColor clearColor]; 
    CALayer * l = [alternate layer]; 

    [l setMasksToBounds:YES]; 
    UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(82, 20, 228, 53)]; 
    detailLabel.backgroundColor = [UIColor clearColor]; 
    detailLabel.text = [NSString stringWithFormat:@"%@ - %@", articleDateString, entry.blogTitle]; 
    detailLabel.textColor = [UIColor grayColor]; 
    detailLabel.font = cellFont2; 
    alternatelabel.font = cellFont; 

    alternatelabel.text = entry.articleTitle; 

    [cell.contentView addSubview:alternate]; 
    [cell.contentView addSubview:alternatelabel]; 
    [cell.contentView addSubview:detailLabel]; 
    [cell.contentView addSubview:month]; 
    [cell.contentView addSubview:date]; 
    [detailLabel release]; 
    [alternatelabel release]; 
    [alternate release];   

} 




return cell; 
} 

回答

1

你错了利用细胞再利用机制。使用该模板

#define TAG_MONTH 1001 

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

static NSString *CellIdentifier = @"Cell"; 

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
// any other views can be added to cell here 

    UILabel *month = [[UILabel alloc] initWithFrame:CGRectMake(8, 10, 53, 21)]; 
    month.font = cellFont3; 
    month.textAlignment = UITextAlignmentCenter; 
    month.backgroundColor = [UIColor clearColor]; 
    month.tag = TAG_MONTH; 

} 

// here you should fill all fields of cell with "entry" or make it empty 
NSString currentmonth = @"something"; 
UILabel *month = [cell.contentView viewWithTag:TAG_MONTH]; 
    month.text = currentmonth; 


return cell; 
} 

其他时刻:

  1. 你不应该初始化格式化环路,初始化一次。
  2. Exctract它变成新方法

    如果([monthfromdate isEqualToString:@ “01”]){
    self.currentmonth = @ “一月”;
    }

    和优化,它可以更简单

+0

当我将所有代码放入if(cell == nil)括号外时,它会在每次滚动新单元格内容时被重写。不确定在回答中#2之后的含义。 – user717452

+0

你不应该把所有的代码都放在外面,只有那些用模型设置单元的部分。部分为细胞添加子视图应在“如果” – NeverBe

+0

我更新了一点我的答案,让你更好地理解。你应该用这种方式来填充单元格中的所有字段 – NeverBe

0

检查该方法返回的数量,可能是其返回2

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 
+0

它返回用于此阵列的计数。 – user717452

相关问题