简体   繁体   中英

Can't set Custom UITableViewCell property - iOS

First of all I want to apologize for my bad english.

I'm having trouble to set the properties of my custom UITableViewCell (HistoricoCell).

When I try to set a property of my cell I get: Signal SIGABRT error:

-(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;

}

I followed a lot of tutorials and questions on the web but I stil with my error.

Can someone help me?

My code:

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

You should set cell indentifier "CellIdentifier" for your cell in File Inspector

Or register your nib file if you add cell with nib:

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

I think your problem is in your cell creation: you try to dequeue a cell if it exists (ie recycle a previously used cell). that is OK, but, especially when the TableView is displayed for the first time, no previously used cell for this table exists. So, you have to create one if the dequeueReusableCellWithIdentifier call return 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM