2012-11-01 61 views
0

我想推入一個可變數組到UITableView.But我的UITableView不會更新,當我按下按鈕。 tableview保持空白......我已經創建了NSLog的tableitems和它們的數量。輸出中顯示的值是正確的。不知道錯誤是什麼。將可變數組推入表視圖

的ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{ 
    UILabel *lbl; 
    NSTimer *stopTimer; 
    NSDate *startDate; 
    BOOL running,lap; 
    UIButton *bttn; 
    NSMutableArray *tableItems; 
    NSString *timeString; 
} 

@property (strong,nonatomic) IBOutlet UILabel *lbl; 
@property (strong,nonatomic) IBOutlet UIButton *bttn; 
@property (strong,nonatomic) NSMutableArray *tableItems; 
@property (strong,nonatomic) NSString *timeString; 

-(IBAction)startPressed:(id)sender; 
-(IBAction)resetPressed:(id)sender; 

-(void)updateTimer; 

@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize lbl,bttn,tableItems,timeString; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    lbl.text = @"00.00.00.000"; 
    running = FALSE; 
    lap = FALSE; 
    startDate = [NSDate date]; 
    tableItems = [[NSMutableArray alloc] init]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
-(IBAction)startPressed:(id)sender{ 
    if(!running){ 
     running = TRUE; 
     lap = TRUE; 
     [sender setTitle:@"Stop" forState:UIControlStateNormal]; 
     [bttn setTitle:@"Lap" forState:UIControlStateNormal]; 
     if (stopTimer == nil) { 
      stopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 
                 target:self 
                 selector:@selector(updateTimer) 
                 userInfo:nil 
                 repeats:YES]; 
     } 
    }else{ 
     running = FALSE; 
     lap = FALSE; 
     [sender setTitle:@"Start" forState:UIControlStateNormal]; 
     [bttn setTitle:@"Restart" forState:UIControlStateNormal]; 
     [stopTimer invalidate]; 
     stopTimer = nil; 
    } 

} 
-(void)updateTimer{ 
    NSDate *currentDate = [NSDate date]; 
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate]; 
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"HH:mm:ss.SSS"]; 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]]; 
    timeString=[dateFormatter stringFromDate:timerDate]; 
    lbl.text = timeString; 
} 

-(IBAction)resetPressed:(id)sender{ 
    if (!lap) { 
     [stopTimer invalidate]; 
     stopTimer = nil; 
     startDate = [NSDate date]; 
     lbl.text = @"00.00.00.000"; 
     running = FALSE; 
    } 
    else{ 
     [tableItems addObject:timeString]; 
     NSLog(@"%@",tableItems); 
     NSLog(@"%i",tableItems.count); 
    } 

} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return tableItems.count; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    //Step 1:Check whether if we can reuse a cell 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 

    //Step2: If there are no new cells to reuse,create a new one 
    if(cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"]; 
    } 

    //Step 3: Set the cell text content 
    cell.textLabel.text = [tableItems objectAtIndex:indexPath.row]; 

    //Step 4: Return the row 
    return cell; 

} 
@end 

有聯繫的數據源,並委託在界面生成器的實現代碼如下。不知道我的錯誤是什麼...

+0

如何以及在哪裏加載表格? – user427969

+0

[tableItems addObject:timeString]; – lakesh

+0

你有什麼問題?你從未說過這個問題。 – rmaddy

回答

2

將對象添加到tableItems數組將不會加載表。

你需要調用[tableView reloadData];

而且,對於這一點,你需要在你的接口文件來創建表的出口。

+0

你在哪裏添加[tableView reloadData];? – lakesh

+0

將對象添加到數組後 – user427969

+0

解決了我的錯誤...謝謝... – lakesh