2015-05-15 38 views
0

首先,我想为我的坏英语道歉。无法设置自定义UITableViewCell属性 - iOS

我很难设置我的自定义UITableViewCell(HistoricoCell)的属性。

当我尝试设置我的细胞的性质,我得到:信号SIGABRT错误:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
// Dequeue the cell. 

HistoricoCell *cell = (HistoricoCell *)[self.tblHistorico dequeueReusableCellWithIdentifier:@"CellIdentifier" forIndexPath:indexPath]; 

// Fetch Item 
NSDictionary *item = [self.dbManager.arrColumnNames objectAtIndex:indexPath.row]; 

// Configure Table View Cell 
[cell.lblCodigo setText:[NSString stringWithFormat:@"%@", item[@"codigo"]]]; 
[cell.btnFavoritar addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside]; 

return cell; 

}

我跟很多教程,并在网络上的问题,但我有我的错误STIL 。

有人可以帮助我吗?

我的代码:

HistoricoCell.h

#import <UIKit/UIKit.h> 

@interface HistoricoCell : UITableViewCell 

@property (weak, nonatomic) IBOutlet UILabel *lblCodigo; 
@property (weak, nonatomic) IBOutlet UIButton *btnFavoritar; 

@end 

SecondViewController.h

#import <UIKit/UIKit.h> 

@interface SecondViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> 

@property (weak, nonatomic) IBOutlet UITableView *tblHistorico; 

SecondViewController.m

#import "SecondViewController.h" 
#import "DBManager.h" 
#import "HistoricoCell.h" 

@interface SecondViewController() 

@property (nonatomic, strong) DBManager *dbManager; 

@property (nonatomic, strong) NSArray *arrPeopleInfo; 

-(void)loadData; 


@end 

@implementation SecondViewController 

static NSString *CellIdentifier = @"CellIdentifier"; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    // Make self the delegate and datasource of the table view. 
    self.tblHistorico.delegate = self; 
    self.tblHistorico.dataSource = self; 

    // Initialize the dbManager property. 
    self.dbManager = [[DBManager alloc] initWithDatabaseFilename:@"bernoullidb.sql"]; 
    [self.tblHistorico registerClass:[HistoricoCell class] forCellReuseIdentifier:@"CellIdentifier"]; 
    [self loadData]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(void)loadData{ 
    // Form the query. 
    NSString *query = @"select * from tbHistorico"; 

    // Get the results. 
    if (self.arrPeopleInfo != nil) { 
     self.arrPeopleInfo = nil; 
    } 
    self.arrPeopleInfo = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]]; 

    // Reload the table view. 
    //[self.tblHistorico reloadData]; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
} 


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return self.arrPeopleInfo.count; 
} 


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    return 60.0; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Dequeue the cell. 

    HistoricoCell *cell = (HistoricoCell *)[self.tblHistorico dequeueReusableCellWithIdentifier:@"CellIdentifier" forIndexPath:indexPath]; 

    // Fetch Item 
    NSDictionary *item = [self.dbManager.arrColumnNames objectAtIndex:indexPath.row]; 

    // Configure Table View Cell 
    [cell.lblCodigo setText:[NSString stringWithFormat:@"%@", item[@"codigo"]]]; 
    [cell.btnFavoritar addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside]; 

    return cell; 
} 

- (void)didTapButton:(id)sender { 
    NSLog(@"%s", __PRETTY_FUNCTION__); 
} 

@end 
+0

您的标签和按钮未正确连接到您的手机, – aBilal17

+0

您能否显示您的应急记录? –

回答

0

你应该设置单元格indentifier “CellIdentifier” 为您在文件检查器[R细胞

或者,如果你与笔尖补充细胞注册您的笔尖文件:

UINib *itemNib = [UINib nibWithNibName:@"yourCell" bundle:nil]; 
[self.tableView registerNib:itemNib forCellReuseIdentifier:@"yourCellReuseIndentifier"]; 
+0

感谢您的答案,但它仍然无法正常工作。 我的单元格的标识符是“CellIdentifier”,在你回答之后,我把下面的代码放在viewDidLoad里面: UINib * itemNib = [UINib nibWithNibName:@“CellIdentifier”bundle:nil]; [self.tblHistorico registerNib:itemNib forCellReuseIdentifier:@“CellIdentifier”]; –

+0

源代码:https://github.com/igorbastos/bernoulliVideos –

+0

你还没有链接到github上的sqlite文件 –

0

我觉得你的问题是在你的创作:你试试,如果它存在,出列单元格(即回收以前使用的单元格)。这是可以的,但是,特别是在第一次显示TableView时,不存在以前用于此表的单元格。因此,如果dequeueReusableCellWithIdentifier调用返回nil,则必须创建一个。

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

    // Dequeue the cell. 
    HistoricoCell *cell = (HistoricoCell *)[self.tblHistorico dequeueReusableCellWithIdentifier:@"HistoricoCellIdentifier" forIndexPath:indexPath]; 

    if(cell == nil) // no queuded cell to dequeue 
    { 
     // you have to create a fresh new one 
     cell = [HistoricoCell alloc] initWithStyle:<your cell style> reuseIdentifier:@"HistoricoCellIdentifier"]; 
    } 

    // Fetch Item 
    NSDictionary *item = [self.dbManager.arrColumnNames objectAtIndex:indexPath.row]; 

    // Configure Table View Cell 
    [cell.lblCodigo setText:[NSString stringWithFormat:@"%@", item[@"codigo"]]]; 
    [cell.btnFavoritar addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside]; 

    return cell; 
} 
+0

感谢您的回复,但单元格不是零。 'if'块没有被调用。 –