IOS:无法在UICell中访问我的标签

时间:2014-03-26 05:35:07

标签: ios objective-c uitableview nsstring

我正在尝试使用原型单元格创建一个表 因为我采取了tableview并在表格视图中插入了一个单元格 在单元格中我已经采用了一个标签,我想从我的Employee类中设置值。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RWTCell *cell = (RWTCell*)[tableView dequeueReusableCellWithIdentifier:@"employeeeCell"];
    if (!cell){
        cell = [[RWTCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"employeeeCell"];
    }
    Employee *currentEmp=[employeeArray objectAtIndex:indexPath.row];
    NSLog(@"  %@",currentEmp.empName);
    cell.lName.text=currentEmp.empName;
//    cell.textLabel.text = currentEmp.empName;

    return cell;

}

以上是我的代码 当我尝试做的时候

RWTCell *cell = (RWTCell*)[tableView dequeueReusableCellWithIdentifier:@"employeeeCell"];

它总是给我null,下面的这一行永远不会起作用

cell.lName.text=currentEmp.empName;

我的Employee.m课程

#import "Employee.h"

@implementation Employee
    @synthesize empDepart,empDoj,empDob,empName,empSalary,empGender;
@end

和Employee.h

#import <Foundation/Foundation.h>

@interface Employee : NSObject
@property (nonatomic, retain) NSString *empName;
@property (nonatomic, retain) NSString *empSalary;
@property (nonatomic, retain) NSString *empDob;
@property (nonatomic, retain) NSString *empDoj;
@property (nonatomic, retain) NSString *empGender;
@property (nonatomic, retain) NSString *empDepart;

@end

修改 对不起,我忘了提起我的班级 我正在制作故事板

#import "RWTCell.h"

    @implementation RWTCell
    @synthesize lName;
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            // Initialization code
        }
        return self;
    }

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated
    {
        [super setSelected:selected animated:animated];

        // Configure the view for the selected state
    }

    @end



#import <UIKit/UIKit.h>

@interface RWTCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UILabel *lName;
@end

2 个答案:

答案 0 :(得分:0)

确保已将自定义单元格与RWTCell.xib上的视图相关联

在你的.h文件

@property(nonatomic,strong) RWTCell *rwtcell;

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

    static NSString *MyIdentifier= @"MyIdentifier";
    RWTCell *cell = (RWTCell*)[tableView   dequeueReusableCellWithIdentifier:MyIdentifier];
    if(cell==nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"RWTCell" owner:self options:nil];
        cell=self.rwtcell;
    }
    Employee *currentEmp=[employeeArray objectAtIndex:indexPath.row];
    NSLog(@"  %@",currentEmp.empName);
    cell.lName.text=currentEmp.empName;
    return cell;
    }

希望它可以帮助你..

答案 1 :(得分:0)

看看我在我的应用程序中使用的自定义单元格。

自定义单元格的

.h文件

#import <UIKit/UIKit.h>

@interface EvalHistoryCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *lblEvaluator;
@property (strong, nonatomic) IBOutlet UILabel *lblDate;
@property (strong, nonatomic) IBOutlet UILabel *lblStore;
@property (strong, nonatomic) IBOutlet UILabel *lblStatus;

@end

自定义单元格的.m文件

#import "EvalHistoryCell.h"

@implementation EvalHistoryCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
@end

在你要使用它的主视图控制器中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *strEvalIdentifier = @"HistoryIdentifier";

 EvaluationListEntity *objEvalEntity=[arrmEvalList objectAtIndex:indexPath.row];

  EvalHistoryCell *cell = (EvalHistoryCell *)[tableView dequeueReusableCellWithIdentifier:strEvalIdentifier];

 if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"EvalHistoryCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
 cell.lblEvaluator.text=objEvalEntity.strEvaluatorName;
  return cell;
}

试试这个。