2012-06-20 24 views
0

在viewDidLoad中,我有這樣的代碼:NSObject的和的TableView調用

ProvRec *provRec = [[ProvRec alloc]init]; 
provRec.status = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,3) 
          ]; 
provRec.desc = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,4) 
          ]; 
[listOfItems addObject:provRec]; 

我應該如何調用 的cellForRowAtIndexPath來顯示TableView中這些記錄:(NSIndexPath *)indexPath

回答

2

做到這一點的方式是通過執行table view datasource protocol。最關鍵的方法如下:

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    ProvRec *provRec = [listOfItems objectAtIndex:indexPath.row]; 
    cell.textLabel.text = provRec.status; 
    cell.detailTextLabel.text = provRec.desc; 
    return cell; 
} 

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

    return [listOfItems count]; 
} 

會有變化,如果你有在表中的多個部分,或查看多個表。但這是基本的想法。

+0

謝謝,我明白了。 – Sunny

相關問題