2013-03-18 98 views
0

我想創建一個UITableView在UIViewController(我會使用UITableViewController但我希望UITableView只能使用一半的屏幕空間),將創建一個新的自定義每次按下一個按鈕時,顯示該單元格中的當前時間以及自上次按下該按鈕以來的時間(因此該單元格中的時間減去該單元格上方的時間)。 問題是,當我按下按鈕時,沒有任何反應,或者如果我沒有初始化爲UITableView創建的屬性,就會出現一個空白單元格。不能動態創建我的自定義單元格UITableView

我的單元格中有3個UILabel's,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){ 
      [email protected]""; 
     } 
     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 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 

感謝您的幫助:)

回答

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

希望它可以幫助你

+0

非常感謝您的指點看法出那些錯誤:)我已經糾正它們,但它仍然無法正常工作。我已經在我的 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)部分放置了一個帶有隨機文本的NSLog,以查看它是否正在運行,並且該應用程序甚至沒有運行該方法,您是否有任何想法爲什麼?? – dietbacon 2013-03-18 02:48:25

+0

@dietbacon用你最新的代碼更新你的問題。 – rmaddy 2013-03-18 02:53:05

+0

oooops,nvm,它現在正在工作,我剛剛刪除了UITableView的初始化代碼。非常感謝你!!!!我喜歡SO的人是如何幫助的:) – dietbacon 2013-03-18 02:55:30

相關問題