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つのラベルには何も表示されません。

4

2 に答える 2

0

あなたのアプローチは正しくありません。セルが再利用されている場合、この特定のインデックスパスセルが次に表示されるときに、インデックスパス{0,1}上のセルが再利用されるとは限りません。同じ識別子のセルが使用され、異なるインデックスパスにある異なるセルのデータが含まれる場合があります。アプローチは、デキュー後のセルの存在に応じてセルをデキューまたは初期化することですが、常にインデックスパスに基づいてセルにデータを割り当てます。

アドバイスのいくつか:cellForRowAtIndexPathメソッド内でNSDateFormatterオブジェクトを作成/解放しないでください。それはあなたにパフォーマンスの問題を与えるでしょう。また、整理のために、UITableViewCellをサブクラス化し、インターフェイスを適切に作成してください。それらをその場で追加するのは面倒で混乱を招きます。

また、何をしようとしているのかを確認するには、NSDateFormatterのmonthSymbolsメソッドとweekdaySymbolsメソッド、およびNSDateComponentのドキュメントを確認することをお勧めします。月を取得するための12個の「if」を使用する代わりに、月と日の情報を取得できます。

于 2012-09-28T22:05:20.113 に答える
0

私はそれが解決策かもしれないと思うので、これを答えに移します(うまくいけば)。

レイアウトサブビューメソッドにラベルがあるため、ラベルが正しく表示されていないと思います。このメソッドは、setFrameの呼び出し、addSubviewの呼び出し、ビューのサイズ変更などを行うと自動的に呼び出されます。cellForRowAtIndexPathでこれらの呼び出しを行っていないため、OSがレイアウトサブビュー呼び出しを自動的に行っていません(これが理にかなっており、正しいことを願っています)。もし私があなたなら、テーブルセル用に別のXIBがあり、管理がはるかに簡単です。

于 2012-09-29T07:41:05.917 に答える