2013-07-08 40 views
12

Trong bảng phân cảnh tôi đã thêm chế độ xem bảng vào bộ điều khiển chế độ xem của mình, tôi đã ctrl kéo TableView đến Viewcontroller và kết nối "delegate" và "datasource". Trong tập tin (.h) tôi đã thêm <UITableViewDataSource,UITableViewDelegate> nhưng khi tôi chạy ứng dụng, tôi chỉ gặp lỗi SIGABRT (?) Và ứng dụng gặp sự cố. Tôi nên làm gì?Làm thế nào để sử dụng TableView bên trong viewcontroller?

+0

lỗi thứ: #import #import "AppDelegate.h" int main (int argc, char * argv []) { @ autoreleasepool { trả về UIApplicationMain (argc, argv, nil, NSStringFromClass ([Lớp AppDelegate])); } } – b3rge

+0

Bạn đã triển khai các phương thức được yêu cầu của UITableViewDataSource chưa? – ColdLogic

+0

Không, tôi không @ColdLogic – b3rge

Trả lời

19

Cho đến giờ rất tốt, bạn chỉ cần triển khai UITableViewDataSource và UITableViewDelegate trong tệp triển khai của bạn.

Các chức năng bắt buộc như sau;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [regions count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Number of rows is the number of time zones in the region for the specified section. 
    Region *region = [regions objectAtIndex:section]; 
    return [region.timeZoneWrappers count]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    // The header for the section is the region name -- get this from the region at the section index. 
    Region *region = [regions objectAtIndex:section]; 
    return [region name]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *MyIdentifier = @"MyReuseIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]]; 
    } 
    Region *region = [regions objectAtIndex:indexPath.section]; 
    TimeZoneWrapper *timeZoneWrapper = [region.timeZoneWrappers objectAtIndex:indexPath.row]; 
    cell.textLabel.text = timeZoneWrapper.localeName; 
    return cell; 
} 

Here's the link for the Apple documentation

+0

Cảm ơn, điều đó đã giúp tôi rất nhiều! – b3rge