使用具有子视图的自定义单元格的UITableViewDataSource
当在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'是油漆过的!
- 参考 :'0 9'是行#0('0')和行#9('9')
题
- 为什么添加子视图会导致绘画问题?
- 将自定义视图绘制到单元格中的正确方法是什么?
上下文
- 我实际的原始代码使用自定义表格视图中的自定义单元格
- 这里的代码示例是源代码的缩写版本。 我可以发布任何需要帮助解决这个问题!
小区标识符的问题必须是唯一的
func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) – > UITableViewCell {
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 }
Dizzle,创build自定义单元格,并在运行时使用Control以及来自Xib。
1)在Xib或Storyboard中的ViewController中创buildCustomCell。
2)给你的细胞标识符
3)在你的cellForRowAtIndexpath数据源的tableview方法中添加下面的代码
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如果不使用Storyboard,则创buildUITableviewCell的自定义扩展。
//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
基于Rajesh Balam的答案这里是我的原始代码从我的UITableViewDataSource的工作forms!
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! 🙂