2012-09-21 4 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. 당신은 초기화 포맷 루프에서, 한 번 init을해서는 안된다.
  2. Exctract 그 새로운 방법으로

    경우 ([monthfromdate isEqualToString : "01"@]) {
    self.currentmonth = @ "1월";
    }

    및 최적화, 그것이 사용되는 어레이의 개수를 반환

+0

if (셀 == nil) 대괄호 밖의 모든 코드를 넣을 때마다 새 셀 내용을 스크롤 할 때마다 다시 작성됩니다. 답변에서 # 2 뒤에 무엇을 의미하는지 확신 할 수 없습니다. – user717452

+0

외부에 모든 코드를 넣지 말고, 모델로 셀을 설정하는 부분 만 넣어야합니다. 셀에 하위 뷰를 추가하는 부분은 "if"에 있어야합니다. – NeverBe

+0

조금 더 잘 이해할 수 있도록 대답을 업데이트했습니다. 그 방법을 사용하여 셀의 모든 필드를 채우십시오. – NeverBe

0

확인이 방법은 반환 수는 될 수는 2

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

간단 할 수있다. – user717452