如何使添加到 UITableViewCell 内容 View 的 UIView 适合其边界?
也就是说,我已经创建了一个 NIB 文件(上面有 3 个标签),并且想用它来显示 UITableView 中的每个单元格。我在 cellForRowAtIndexPath 方法中将基于 NIB 的自定义 View 添加到单元格的内容 View 中,但是我看到的最终结果是一 (1) 个基于 NIB 的自定义 View (不是我预期的在 tableview 中有多个)。
我该如何安排,以便每个自定义 View 都整齐地适合每个 UITableViewCell?另请注意,自定义 NIB View 中的标签具有自动换行功能。
我是否必须为自定义 NIB View 创建框架,但在那种情况下我不确定如何设置坐标。它会与 UITableViewCell 相关吗?如果自定义 View 高度由于它的 UILabel 自动换行而改变,那么我假设我必须分别在 UITableView 中手动计算这个高度? (也许我应该转到动态/自定义 View 创建并放弃使用 InterfaceBuilder/NIB 的概念)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIView *detailedView = [[[NSBundle mainBundle] loadNibNamed:@"DetailedAppointView" owner:self options:nil] objectAtIndex:0];
[cell.contentView addSubview:detailedView]; // DOESN'T SEE TO WORK
return cell;
}
最佳答案
如果我需要实现自定义 UITableViewCell,我就是这样做的。
我为我的自定义单元格 ex 创建了一个 UITableViewCell
的子类。 “我的自定义单元格”。然后我为它创建一个 NIB 文件,在这个 NIB 文件中我插入 UITableViewCell
并将它的类型更改为“MyCustomCell”,我给它一个与类名同名的标识符。然后我将所有 subview 插入到我的单元格中。全部在 IB 中。
在我按照自己的喜好设置好我的手机后 :-) 我按以下方式使用它:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyCustomCell";
static NSString *CellNib = @"MyCustomCell";
MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (MyCustomCell *)[nib objectAtIndex:0];
}
//Manipulate your custom cell here
return cell;
}
关于iphone - 如何使添加到 UITableViewCell 内容 View 的 UIView 适合其范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6324831/