Ios 将UITableViewDataSource与具有子视图的自定义单元格一起使用

Ios 将UITableViewDataSource与具有子视图的自定义单元格一起使用,ios,uitableview,uikit,Ios,Uitableview,Uikit,在UITableView中使用自定义单元格时,我得到一个奇怪的表格重叠: 问题 向下滚动&最后两行将顶部两行绘制在其上 向上滚动&前两行画了两行 以下是UITableViewDataSource的代码: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell : UITableViewCell = tab

在UITableView中使用自定义单元格时,我得到一个奇怪的表格重叠:

问题

  • 向下滚动&最后两行将顶部两行绘制在其上
  • 向上滚动&前两行画了两行
以下是UITableViewDataSource的代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!;

    let cellText:String = self.items[indexPath.row];

    let subjectField:UILabel = UILabel(frame: CGRect(x:55, y: 25, width: 303, height:  25));
    
    subjectField.text = cellText;
    
    //cell.textLabel?.text = cellText;    //works fine!
    cell.addSubview(subjectField);        //fails  XX(!
    
    return cell;
}
以下是一个屏幕截图:

  • 注意:“0 9”和“1 A”是画外画
  • Ref:'09'是第0行(“0”)和第9行(“9”)

问题

  • 为什么添加子视图会导致绘制问题
  • 在单元中绘制自定义视图的正确方法是什么
上下文

  • 我的实际原始代码在自定义表视图中使用自定义单元格
  • 这里的代码示例是原始代码的浓缩版本。我可以发布任何需要帮助解决这个问题的东西
单元标识符的问题必须是唯一的

func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{

    let  cellId:String = "Cell"+String(indexPath.row)
    var cell  = tableView.dequeueReusableCellWithIdentifier(cellId)
    if (cell == nil){
        cell = UITableViewCell(style: .Default, reuseIdentifier:cellId)
    }
    let cellText:String = String(indexPath.row)
    let subjectField:UILabel = UILabel(frame: CGRect(x:55, y: 25, width: 303, height:  25))

    subjectField.text = cellText

    (cell! as UITableViewCell).addSubview(subjectField)

    return cell! as UITableViewCell

}
眩晕 ,在运行时以及从Xib创建自定义单元格并使用控件

1) 在ViewController中的Xib或故事板中创建CustomCell

2) 为您的手机提供一个标识符

3) 在tableview的cellforrowatinexpath数据源方法中添加以下代码

    static NSString *cellId = @"CustomIdenfier";
    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:firstCellId];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createCustomCell:cell atIndexPath:indexPath];
    }
    [self updateCustomCell:cell atIndexPath:indexPath];
    return cell;
4) createCustomCell方法如果在Xib中添加控件,则不需要此方法

-(void)createCustomCell:(CustomCell*)cell atIndexPath:(NSIndexPath*)indexPath{
    //Here I am adding custom label run time, you can add
    // Any control in Xib and create a reference IBOutlet for it and user here.
    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, cell.contentView.frame.size.width-10, 30)];
    lblTitle.textColor = [UIColor blackColor];
    lblTitle.tag = 1;
    [cell.contentView addSubview:lblTitle];
}
5) updateCustomCell方法

-(void)updateCustomCell:(CustomCell*)cell atIndexPath:(NSIndexPath*)indexPath{

    UILabel *lblTitle = (UILabel*)[cell.contentView viewWithTag:1];
    [lblTitle setText:[NSString stringWithFormat:@"%i",indexPath.row]];
}
//编辑1 如果不使用故事板,则创建UITableviewCell的自定义扩展

//CustomCell.h
#import <UIKit/UIKit.h>
@interface CustomCell : UITableViewCell
@property (strong, nonatomic) UILabel *lblTitle;
@end


//CustomCell.m
#import "CustomCell.h"
@implementation CustomCell
@synthesize lblTitle;

- (void)awakeFromNib {
    NSLog(@"imgUrl:%@ txtName:%@",imgUser,txtName);
    // Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        //Setup your Cell like initialising variables and setting frames of controls 
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}
@end
//CustomCell.h
#进口
@接口CustomCell:UITableViewCell
@属性(强,非原子)UILabel*lblTitle;
@结束
//CustomCell.m
#导入“CustomCell.h”
@实现自定义单元
@合成枸杞多糖;
-(无效)从NIB中唤醒{
NSLog(@“imgUrl:%@txtName:%@”,imgUser,txtName);
//初始化代码
}
-(id)initWithStyle:(UITableViewCellStyle)样式重用标识符:(nullable NSString*)重用标识符{
self=[super-initWithStyle:style-reuseIdentifier:reuseIdentifier];
如果(自我){
//像初始化变量和设置控件框架一样设置单元格
}
回归自我;
}
-(无效)设置选定:(BOOL)选定动画:(BOOL)动画{
[超级设置选定:选定动画:动画];
//为所选状态配置视图
}
@结束

根据Rajesh Balam的回答,这里是我的UITableViewDataSource原始代码的工作形式

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    /**********************************************************/
    /* @soln  a UNIQUE 'cellId' is the key                    */
    /**********************************************************/
    let  cellId  :String = "Cell" + String(indexPath.row);

    var cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellId);

    if (cell == nil){
        cell = UITableViewCell(style: .Default, reuseIdentifier:cellId); //comment this out and watch it fail!
    }

    let cellText:String = self.items[indexPath.row];

    let subjectField:UILabel = UILabel(frame: CGRect(x:55, y: 25, width: 303, height:  25));

    subjectField.text = cellText;

    (cell! as UITableViewCell).addSubview(subjectField);

    return cell! as UITableViewCell;

}

它是有效的。谢谢Rajesh!!!:)

我发现你的代码有两个明显的问题:1)
dequeueReusableCellWithIdentifier:
可能返回null,因此你需要创建一个新的cell实例。你最好使用
dequeueReusableCellWithIdentifier:forIndexPath:
,如果需要,它会自动分配单元格;2)你正在添加
subjectField
在重用单元格时,反复查看子视图。您应该在XIB中添加它一次,或者检查它是否已添加到您的代码中。我不使用故事板。我是100%编程的,抱歉!感谢您的详细回答!!!J-Dizzle,为以编程方式使用自定义单元格而编辑。我大吃一惊,印象深刻,非常惊讶-您修复了ed it!!!!!!!!!!!!!!!该(cell==nil)是关键。感谢您在此处传达问题来源,然后是解决方案!!!!今晚我请客,谢谢!!!!:)