0

ボタンを押すたびに新しいカスタムセルを作成し、現在の時刻を表示するUIViewControllerでUITableViewを作成しようとしています(UITableViewControllerを使用しますが、UITableViewで画面スペースの半分のみを使用したい)そのセルと最後にボタンを押してからの時間 (つまり、このセルの時間からその上のセルの時間を差し引いたもの)。問題は、ボタンを押しても何も起こらないか、UITableView 用に作成したプロパティを初期化しないと空白のセルが表示されることです。

私のセルには、inOut、entryTime、delta の 3 つの UILabel があり、UITableView 用に作成した IBOutlet は timeTable です。newEntry はボタンです。UITableView のデータソースとデリゲートを UIViewController に設定し、UIViewController のヘッダーでプロトコル UITableViewDataSource、UITableViewDelegate を「呼び出し」ました。何が問題なのかわかりません。

UIViewController の私のコードは次のとおりです。

    @interface MainViewController ()

@property (strong, nonatomic) Brain *brain;
@end

@implementation MainViewController{
    @private
    NSMutableArray *_entryArray;
    NSMutableArray *_timeSinceLastEntryArray;
}
@synthesize brain=_brain;
@synthesize timeTable=_timeTable;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _brain = [[Brain alloc] init];
    _entryArray = [[NSMutableArray alloc] init];
    _timeSinceLastEntryArray = [[NSMutableArray alloc] init];
    _timeTable = [[UITableView alloc] initWithFrame:CGRectZero];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_entryArray count];
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier= @"Cell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    if (indexPath.row%2==0){
        cell.inOut.text = [NSString stringWithFormat:@"Entrada %d", indexPath.row/2 +1];
        cell.entryTime.text = [NSString stringWithFormat:@"%@", [_entryArray objectAtIndex:indexPath.row]];
        if (indexPath.row==0){
            cell.delta.text=@"";
        }
        else {
            cell.delta.text = [_timeSinceLastEntryArray objectAtIndex:indexPath.row];
        }
    }
    else{
        cell.inOut.text = [NSString stringWithFormat:@"Saída %d", (indexPath.row+1)/2];
        cell.entryTime.text = [NSString stringWithFormat:@"%@",[_entryArray objectAtIndex:indexPath.row]];
        cell.delta.text = [_timeSinceLastEntryArray objectAtIndex:indexPath.row];
    }



    return cell;
}



- (IBAction)newEntry:(id)sender {

    [_entryArray addObject:[self.brain currentTime]];
    NSInteger numberOfEntrys= [_entryArray count];
    if (numberOfEntrys >1){
        NSString *timeSinceLastEntry = [_brain timeSince:[_entryArray objectAtIndex:(numberOfEntrys-2)]];
        [_timeSinceLastEntryArray addObject:timeSinceLastEntry];
    }
    else {
        [_timeSinceLastEntryArray addObject:@"0"];
    }

    [_timeTable reloadData];
}


@end

CustomCell の場合:

@implementation カスタムセル

- (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
}

@synthesize  inOut=_inOut, entryTime=_entryTime, delta=_delta;
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {

        // Initialization code

        _inOut = [[UILabel alloc]init];

        _inOut.textAlignment = UITextAlignmentLeft;


        _entryTime = [[UILabel alloc]init];

        _entryTime.textAlignment = UITextAlignmentLeft;


        _delta = [[UILabel alloc]init];

        _delta.textAlignment = UITextAlignmentLeft;

        [self.contentView addSubview:_entryTime];

        [self.contentView addSubview:_inOut];

        [self.contentView addSubview:_delta];

    }

    return self;

}
@end

助けてくれてありがとう :)

4

1 に答える 1

1

初期化コードを入れているのがわかります

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier

しかし、あなたはオブジェクトを作成します

cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

作成したビューにフレームを提供していない

_inOut = [[UILabel alloc]init];
_entryTime = [[UILabel alloc]init];
_delta = [[UILabel alloc]init];

それがあなたを助けることを願っています

于 2013-03-18T02:19:18.520 に答える