gpt4 book ai didi

ios - 单行 UITableView 在滚动之前不显示 detailTextLabel

转载 作者:行者123 更新时间:2023-11-29 02:30:51 25 4
gpt4 key购买 nike

我有一个 UITableView,它是通过 Storyboard创建的,单元格样式设置为“右侧细节”。当且仅当满足以下任一条件时,这会正确显示 textLabel 和 detailTextLabel:

  • 表格 View 中有多行
  • 我将单元格滚动到屏幕外(显然它只是在滚动后弹回,因为它是唯一的项目,但现在它显示正确)
  • 我触摸(突出显示)该项目
  • 我切换到不同的 View 并返回(这是一个选项卡 View 应用程序)

但是,如果我只有一个我没有做任何事情的条目,那么只会显示 textLabel。

我的代码相当基础。我有一个数组,我用制表符分隔的 textLabel/detailTextLabel 值填充它。然后,在我的 tableView:cellForRowAtIndexPath: 函数中,我有以下代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"default"];

NSDictionary *itemDict=[scanArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[itemDict valueForKey:@"quantity"];
cell.textLabel.text=[itemDict valueForKey:@"item"];

[cell setNeedsLayout];
[cell setNeedsDisplay];

return cell;
}

每当我对 scanArray 对象进行更改时,我都会在我的 TableView 上调用 reloadData 函数。这显然有效,因为 textLabel 确实显示在 tableview 中,但是如前所述,detailTextLabel 没有(最初)。

编辑:请注意,表格的内容不是静态的 - 用户将在整个正常使用过程中添加和删除项目。很简单,问题只会在第一项添加到列表后立即出现。一旦用户将第二个项目添加到列表中(或执行我上面提到的任何其他操作),问题就会消失。

还有人指出,将我的数据存储为字典数组而不是制表符分隔的文本会更好。我同意,并将进行更改。

Edit2:表格根据用户点击按钮进行更新。用户输入两个值(项目编号和数量),然后点击按钮。然后运行以下代码:

NSDictionary *itemDict=[NSDictionary dictionaryWithObjectsAndKeys:
self.ItemCode.text,@"item",
self.Quantity.text,@"quantity",
returnedData,@"id", nil];

[scanArray insertObject:itemDict atIndex:0]; //The most recent item is always the first item in the list.
[self.ItemCode setText:@""]; //clear out the entry fields
[self.Quantity setText:@""];

[self.HistoryTable reloadData]; //reload the table view.

我还用这里的数据做了其他事情(特别是将它传输到服务器),但这与 tableView 无关。

最佳答案

我发现一个可行的解决方案是替换行:

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"default"];

与:

UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"default"];

并从 Storyboard 中删除原型(prototype)单元格。然而,我读到这不是最佳做法。

关于ios - 单行 UITableView 在滚动之前不显示 detailTextLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26895201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com