将IBOutlets连接到UITableViewCell原型
我正在用自定义的UITableViewCell原型创build一个UITable。 我的单元格包含UIImageViews,UILabels和UIButtons。
当我控制并从我的button拖到我的class级“界面,它工作得很好。 但是,它不适用于网点。
当我在.h文件中创buildIBOutlet时,只能selectUITable而不是单元格,当然结果是破坏的应用程序。
你们有什么想法如何解决这个问题? 我不想为单元格使用自定义类。 我真的想坚持故事板和原型。
提前致谢
使用带标签的标签将完成工作,但从来没有一个好的做法…最好的方法是创build一个自定义类的UITableViewCell。
即select
新文件> Cocoa Touch> Objective C Class
并创build它作为UITableViewCell的子类现在你将得到.h和.m文件..
下一步是为此创build单元格的视图
select
新build文件>用户界面>空
现在使用你的customcell类的相同名称来创build它(可以说“CustomCell”)
现在你将有三个文件CustomCell.h,CustomCell.m,CustomCell.xib
现在selectxib文件并在xib上添加UITableViewCell对象,并将其自定义类设置为“CustomCell”
看下面的图片
现在,你可以拖动任何东西(UIImageView,UITextfield,UIButton)到下面的视图,并给出定制类别和pipe理使用委托方法的行动..
如果你有imageView出口作为titleImage ..然后你可以访问相同的创build单元格对象CellForRowAtIndex(TableView的方法)设置图像。
cell.titleImage=[UIImage ImageNamed:@"goo.png"];
现在我要说的另外一件事是你必须在CustomCell.m中实现init方法来加载nib >>
它会看起来像下面的代码。
-(id)initWithDelegate:(id)parent reuseIdentifier:(NSString *)reuseIdentifier { if (self = [self initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]) { self=(CustomCell*)[[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil] lastObject]; } self.backgroundColor = [UIColor clearColor]; self.backgroundView = NULL; self.selectedBackgroundView =NULL; //If you want any delegate methods and if cell have delegate protocol defined self.delegate=parent; //return cell return self; }
如果你正在使用你的手机上的button,现在最好有代表
所以在button的操作方法,你可以调用委托方法(通过单元格对象),并实现你的ViewController的委托与TableView
这里是例子
现在你可以使用你的单元格的UITableView来填充…而不是为CustomCell.xib设置reuseIdentifier值(与您设置CustomClass相同)
让我们设置它,嗯还有什么“customCell”
所以在填充tableView的时候使用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *cellIdentifier=@"customCell"; CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if(cell==nil) cell= [[CustomCell alloc] initWithDelegate:self reuseIdentifier:cellIdentifier]; //set cell properties cell.titleImage=[UIImage ImageNamed:@"title.png"]; return cell; }
也不要忘记添加委托方法
给
ViewController:UIViewController<CustomCellDelegate>
在您的ViewController的ViewController.h文件
然后在您的ViewController.m(实现文件)中实现它的正文
如
-(void)cellButtonPressed:(CustomCell*)cell { NSLog(@"Pressed"); }
最好给你的单元格索引属性来处理你的表select取消select方法
提供
@property int index;
在你的CustomCell.h中
并且
加
cell.index=indexPath.row;
在你的tableView cellAtRow委托…..
这看起来像一个很长的方法,但它的安静有用和可读…
——- NB ———-:
如果您有任何alignment问题,只需通过实现返回CustomCell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {}
它可能会发生….
你必须为单元格创build一个自定义的UITableViewCell类,然后只有cntrl +拖动来创build网点是有道理的,其他方面则有一个更简单的方法。 有像99343,99345(为了避免重叠)到你的UIImageViews,UILabels等标签和访问它们像
UILabel *myLabelFromPrototypeCell = (UILabel*)[cell.contentView viewWithTag:99343];