カスタムセルを持つテーブルビューコントローラがあります。各タイプのセルについて、私はストーリーボードとクラスのプロトタイプセルを作成しました。テーブルビューとカスタムセルの予測不可能な動作
は、ここでは、細胞の一つだ:
セルは数が含まれている円形のボタンがあります。
私はこのような私のcellForRowAtIndexPathメソッドに番号の値を変更しようとしている:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
TrackMilstoneCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"TrackMilstoneCell"];
if (cell == nil) {
cell = [[TrackMilstoneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TrackMilstoneCell"];
}
cell.backgroundColor = cell.contentView.backgroundColor;
cell.milestoneNumber.backgroundColor = UIColorFromRGB(0xA875E1);
[cell.milestoneNumber.titleLabel setText:@"2"];
return cell;
} ...
しかし、私は非常に予期しない動作を取得しています。 tableviewがリロードされるたびに、時には1(ストーリーボードのデフォルト)、時には2(私が望むもの)が得られます。
これは私(TrackMilstoneCell)クラスのコードです:
#import "TrackMilstoneCell.h"
@implementation TrackMilstoneCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
-(void)layoutSubviews
{
[self viewSetup];
}
-(void)viewSetup
{
self.milestoneNumber.layer.masksToBounds = NO;
self.milestoneNumber.layer.borderColor = [UIColor whiteColor].CGColor;
self.milestoneNumber.layer.borderWidth = 4;
self.milestoneNumber.layer.cornerRadius = self.milestoneNumber.bounds.size.width/2.0;
}
- (void)awakeFromNib
{
// Initialization code
[super awakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
これはセルを再利用しているため、完全な 'cellForRowAtIndexPath'メソッドを追加する必要があります。 –
cellForRowAtIndexPathのelse部分はどこにありますか?そのセルの行番号が0になっていますか? – Windindi