2013-11-27 103 views
0

我的cellForRowAtIndexPath不會每次啓動 我將dataSourcedelegate設置爲文件所有者。 我也設置了參考插座。 我也嘗試tbl.delegate=self;tbl.datasource=self;但它不起作用。cellForRowAtIndexPath不被調用?

我該怎麼辦?

-(void)load 
    { 
     [name resignFirstResponder]; 
     [salary resignFirstResponder]; 
     [email protected]""; 
     [email protected]""; 
     [tblViewController.data removeAllObjects]; 

     NSString *insertStatement = [NSString stringWithFormat:@"select * from emp"]; 

     sqlite3_stmt *statement; 
     NSString *path= [self getDBPath]; 

     if (sqlite3_open([path UTF8String], &db) == SQLITE_OK) 
     { 
      if (sqlite3_prepare_v2(db, [insertStatement UTF8String], -1,&statement, NULL) == SQLITE_OK) 
      { 
       while (sqlite3_step(statement) == SQLITE_ROW) 
       { 

        NSMutableDictionary *record=[[NSMutableDictionary alloc]init]; 

        NSString *nm= [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,0)]; 

        NSString * sl= [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,1)]; 

        [record setValue:nm forKey:@"name"]; 
        [record setValue:sl forKey:@"salary"]; 

        NSLog(@"%@ %@",nm,sl); 

        [tblViewController.data addObject:record]; 

        NSLog(@"%@",tblViewController.data); 

       } 
       sqlite3_finalize(statement); 
      } 
      sqlite3_close(db); 
     } 
     NSLog(@"%lu",(unsigned long)[tblViewController.data count]); 
     [tblViewController.tbl reloadData]; 
    } 

//此代碼是tableviewcontroller.m的

  #import "tableViewController.h" 
     #import "ViewController.h" 

    @interface tableViewController() 

    @end 

    @implementation tableViewController 
    @synthesize tbl,data; 
    @synthesize viewOfTable; 


    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
    { 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Custom initialization 
    NSLog(@"only for check"); 
    [self setup]; 
    } 
return self; 
    } 

    - (void)viewDidLoad 
    { 
[super viewDidLoad]; 
// Do any additional setup after loading the view from its nib. 
//[self setup]; 
tbl.delegate = self; 
     } 

    - (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 [data count]; 

    } 

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView 
         dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 

    cell = [[UITableViewCell alloc] 
      initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 

} 

cell.textLabel.text=[[data objectAtIndex:indexPath.row] valueForKey:@ 
        "name"]; 
cell.detailTextLabel.text=[[data objectAtIndex:indexPath.row] 
          valueForKey:@"salary"]; 

return cell; 
    } 

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     //name.text =[[data objectAtIndex:indexPath.row] valueForKey:@"name"]; 
     // salary.text=[[data objectAtIndex:indexPath.row] valueForKey:@"salary"]; 
    } 

    - (void)setup 
    { 
//Store the whole view in uiView...mac 
// [[NSBundle mainBundle] loadNibNamed:@"RegistrationViewController" owner:self options:nil]; 
viewOfTable=[[UIView alloc]initWithFrame:self.view.frame]; 

[viewOfTable addSubview:self.view]; 
self.viewOfTable.translatesAutoresizingMaskIntoConstraints=YES; 
    } 

請解決我的問題。 你沒有得到任何東西,請告訴我。

+2

你應該記錄tbl和tableView返回的值:numberOfRowsInSection: – jbat100

+0

什麼時候,具體是不是被調用? – Wain

回答

1

您需要設置Table DataSource以便tableView構造表格行。

#import "tableViewController.h" 
#import "ViewController.h" 

    @interface tableViewController() <UITableViewDataSource,UITableViewDelegate> 

    - (void)viewDidLoad 
    { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    tbl.dataSource = self 
    tbl.delegate = self; 
    } 
0

或者你也可以檢查,如果你有一個錯誤的數據陣列中,因爲如果它的大小爲0,有一個部分沒有行,沒有這些細胞內的行以及和的cellForRowAtIndexPath不會被調用..

0

你應該設置dataSource和delegate。當你這樣做時,檢查tbl。也許它是零? 如果不檢查如果執行

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

檢查什麼值返回

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

沒有更多選項。

相關問題