Joe*_*out 6 uitableview ios detailtextlabel
这个让我把头发拉出来.我只是想用UITableViewCellStyleSubtitle样式创建一个带有文本和细节的表.但细节并未出现.通常这是因为有人忘记用正确的样式初始化单元格,但这不是在这种情况下发生的事情.我已经尝试了所有四种单元格样式,并且细节没有出现在任何单元格中,尽管当我使用UITableViewCellStyleValue2时文本标签确实向右移动.
我以前多次成功创建了表格.在这种情况下,我能想到的唯一重要区别是我没有使用UITableViewController.相反,我像任何其他视图一样嵌入UITableView,并连接我自己的数据源和委托.
我已经把关键方法归结为:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Get the UITableViewCell (out of the recycling pool, or de novo)
NSString *reuseID = @"CELL";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
if (not cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:reuseID] autorelease];
}
cell.textLabel.text = @"Foo";
cell.detailTextLabel.text = @"Bar";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.frame = CGRectMake(10, 20, 200,22); // required
cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:12]; // also required
//[cell setNeedsLayout]; (makes no difference)
return cell;
}
Run Code Online (Sandbox Code Playgroud)
只有当我包含上面"必需"行的两个(并使用除Default之外的任何单元格样式)时,我才能看到详细文本,即使我这样做,文本标签的位置也完全重叠细节标签的位置.
因此,似乎初始化程序正在创建detailTextLabel UILabel,但将其字体大小设置为零,并将其框架设置为空或屏幕外的内容.(我已经尝试在调试器中检查这些,但它不是很有用 - 字体大小为零,框架为空,表示textLabel和detailTextLabel,但textLabel总是显示正常.)
显然,如果必须,我可以通过手动调整标签大小和字体来解决它.但是,在这种情况下,我必须这样做,这通常只是设置样式,文本和布局是自动的,这让我非常不安.我搜索了谷歌和堆栈溢出,但找不到任何类似问题的引用.有没有人知道这里发生了什么?
(在iOS 6.1下测试.)
注册标准的UITableViewCell类会阻止您使用样式化的UITableViewCells,因为此代码块永远不会被执行
if (cell == nil) {
// Cell will never be nil if a class/nib is registered
}
Run Code Online (Sandbox Code Playgroud)
解决方案:您是否在tableView中注册了一个nib或类?如果您有如下所示的行,则应将其删除
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:"cellReuseID"];
Run Code Online (Sandbox Code Playgroud)
并更新您的出列语句,如下所示
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:"cellReuseID"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:"cellReuseID"];
}
Run Code Online (Sandbox Code Playgroud)
我希望这对你有帮助,一旦检查一下
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = @"Foo";
cell.detailTextLabel.text = @"Bar";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.frame = CGRectMake(10, 20, 100,22);
return cell;
}
Run Code Online (Sandbox Code Playgroud)
诸如字体大小、单元格背景颜色更改之类的操作。标签这些都是您必须在下面的方法中执行的功能。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:10];
cell.textLabel.backgroundColor=[UIColor redColor];
cell.textLabel.frame = CGRectMake(10, 20, 100,22);
cell.detailTextLabel.frame = CGRectMake(10, 20, 200,22);
}
Run Code Online (Sandbox Code Playgroud)
如果您在 cellForRowAtIndexPath 方法中更改这些类型的属性,那么您将丢失这些属性。
| 归档时间: |
|
| 查看次数: |
14813 次 |
| 最近记录: |