簡體   English   中英

無法設置自定義UITableViewCell屬性-iOS

[英]Can't set Custom UITableViewCell property - iOS

首先,我要為我的英語不好而道歉。

我在設置自定義UITableViewCell(HistoricoCell)的屬性時遇到麻煩。

當我嘗試設置單元格的屬性時,出現: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;

}

我在網上關注了很多教程和問題,但對錯誤卻不屑一顧。

有人能幫我嗎?

我的代碼:

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

您應該在文件檢查器中為單元格設置單元格標識符“ CellIdentifier”

或注冊您的筆尖文件,如果您添加帶有筆尖的單元格:

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

我認為您的問題出在您的單元格創建中:如果存在單元格,則嘗試使其出隊(即回收先前使用的單元格)。 可以,但是,尤其是在第一次顯示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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM