0

我进行了重大更改并为 TableView 单元设置了一个自定义类。

细胞.h

#import <UIKit/UIKit.h>

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

细胞.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 个标签上没有显示任何内容。

4

2 回答 2

0

你的方法不正确。当单元被重用时,这并不一定意味着索引路径 {0,1} 上的单元将在下次该特定索引路径单元变为可见时被重用。将使用具有相同标识符的单元格,它可能包含不同索引路径上不同单元格的数据。您的方法应该是根据出队后单元格的存在情况对单元格进行出队或初始化,但始终根据索引路径在单元格上分配数据。

几点建议:不要在 cellForRowAtIndexPath 方法中创建/释放 NSDateFormatter 对象。它会给你带来性能问题。此外,出于组织目的,请继承 UITableViewCell 并正确创建接口。即时添加它们是混乱和混乱的。

另外,看看你想要做什么,你可能想看看 NSDateFormatter 的 monthSymbols 和 weekdaySymbols 方法,以及NSDateComponent 文档。您可以获得月份和日期信息,而不是使用 12 个“ifs”来获取月份。

于 2012-09-28T22:05:20.113 回答
0

将其移至答案,因为我认为这可能是解决方案(希望如此)。

我认为您的标签显示不正确,因为您在布局子视图方法中有它们。当您执行诸如调用 setFrame、addSubview 或调整视图大小之类的操作时,会自动调用此方法。您没有在 cellForRowAtIndexPath 中进行任何这些调用,因此操作系统不会自动进行布局子视图调用(我希望这是有道理且正确的)。如果我是你,我会有一个单独的 XIB 用于表格单元格,它更容易管理。

于 2012-09-29T07:41:05.917 回答