在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 :(得分:1)
单元格标识符的问题必须是唯一的
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
}
答案 1 :(得分:1)
Dizzle ,创建自定义单元格并在运行时和Xib中使用Control。
1)在Xib或Storyboard中的ViewController中创建CustomCell。
提供标识符3)在tableview的 cellForRowAtIndexpath 数据源方法中添加以下代码
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,则创建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
答案 2 :(得分:0)
基于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;
}
有效。谢谢拉杰什! :)