0

我正在尝试在 UIViewController 中创建一个 UITableView (我会使用 UITableViewController 但我希望 UITableView 只使用一半的屏幕空间),每次按下按钮时都会创建一个新的自定义单元格,并显示当前时间在该单元格中以及自我上次按下按钮以来的时间(因此该单元格中的时间减去其上方单元格中的时间)。问题是,当我按下按钮时,没有任何反应,或者,如果我没有初始化为 UITableView 创建的属性,则会出现一个空白单元格。

我的单元格中有 3 个 UILabel,inOut、entryTime 和 delta,我为 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

对于自定义单元:

@implementation CustomCell

- (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];

并且您没有将 Frame 提供给您创建的视图

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

希望对你有帮助

于 2013-03-18T02:19:18.520 回答