无法设置自定义UITableViewCell属性 - iOS

时间:2015-05-15 11:43:34

标签: ios objective-c uitableview

首先,我想为我糟糕的英语道歉。

我无法设置自定义UITableViewCell(HistoricoCell)的属性。

当我尝试设置我的单元格的属性时,我得到:Signal 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;

}

我在网上关注了很多教程和问题但我仍然犯了错误。

有人可以帮助我吗?

我的代码:

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

2 个答案:

答案 0 :(得分:0)

你应该设置单元格标识符&#34; CellIdentifier&#34;对于File Inspector中的单元格

如果使用nib添加单元格,则注册nib文件:

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

答案 1 :(得分: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;
}