【发布时间】:2015-02-13 01:50:42
【问题描述】:
我在创建我需要的自定义表格视图时遇到了一个有趣的问题...
我找到了这个question,但它并没有真正解决我的问题...
我试图将一个子类 UIView 放在一个子类 UITableViewCell 中。自定义视图包含一个 UIButton 和几个标签。简化,是这样的:
自定义视图和自定义 tableviewcell 都有 xib。
MyCustomView.xib 的类设置为 MyCustomView,我有标签和按钮的属性以及按钮的 IBAction。
MyCustomTableView.xib 具有 MyCustomView 的属性,并且正在导入 MyCustomView.h。
在 MyCustomView.xib 中,我有这个初始化:
-(id)initWithNibName:(NSString *)nibName nibBundle:(NSBundle *)nibBundle myLabelText:(NSString *)labelText {
//The custom view in the tableviewcell is 69 by 64...
if ((self = [super initWithFrame:CGRectMake(0, 0, 69, 64)])) {
[self setmyLabelText:labelText];
}
return self;
}
在我的 TableViewController 中...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomTableViewCell *cell = (MyCustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"theCell" forIndexPath:indexPath];
// Configure the cell...
cell.customView = [[MyCustomView alloc] initWithNibName:@"MyCustomView" nibBundle:nil fileContentID:@"Some Text..."];
return cell;
}
当我运行应用程序时,自定义 tableview 单元格的内容很好,但是自定义 tableview 单元格内的自定义视图内容是空白的。
【问题讨论】:
-
算了,我把评论删了,没看懂你的意思,在柏林已经很晚了;)你的也删了
-
您无法在 initWithNibName 中设置标签的文本,因为尚未连接出口。此外,您永远不会将自定义视图添加到单元格中,您只需分配初始化它。无论如何,您在 cellForRowAtIndexPath 中这样做也会遇到麻烦,因为每当重用单元格时,您都会添加一个新视图。你真的需要重新考虑你的方法。
-
为什么你做的太复杂了?在情节提要中,使用您在 tableView 中的需求设计创建一个自定义的 tableviewCell。然后创建一个 UItableViewCell 子类并连接到单元格。然后,将 IBoutlet 连接到视图。
标签: ios objective-c uitableview