我有一个显示 CoreData FetchRequest 的列表,并且有一个可以更改列表排序方式的选取器。我目前的实现方式如下:
struct ParentView: View {
enum SortMethod: String, CaseIterable, Identifiable {
var id: Self { self }
case byName = "Name"
case byDateAdded = "Date Added"
}
@State private var currentSortMethod = SortMethod.byName
var body: some View {
ItemListView(sortMethod: currentSortMethod) // See child view implementation below
.toolbar {
ToolbarItem(placement: .principal) {
Picker("Sort by", selection: $currentSortMethod) {
ForEach(SortMethod.allCases) { sortMethod in
Text(sortMethod.rawValue)
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
子视图如下所示:
struct ItemListView: View {
@Environment(\.managedObjectContext) private var managedObjectContext
@FetchRequest …Run Code Online (Sandbox Code Playgroud) 我需要一点NSTableView和动态行高的帮助
是)我有的:
基于单列视图的NSTableVIew绑定到数组控制器.每个NSTableCellView包含三个子视图:NSImageView,NSTextField(单行)和NSTextField(多行).基本上,这是一个聊天界面,所以你会有一个消息,发件人和头像的列表.
我想要实现的目标:
当文本长于行的最小高度时,行会展开以适合内容.就像iMessage一样,气泡扩展到适合消息.
......这似乎是一件非常自然的事情,但是我在网上找到的所有相关解决方案(参考文献1,参考文献2),其中没有一个对我有用.
Ref 2看起来很奇妙,但这些都不适用于我的应用程序,因为示例项目使用第三方自动布局代码,整个内容是为iOS设计的.参考文献1提供了一个非常有前途的解决方案,用英语写成.我尝试使用解决方案中描述的"虚拟视图"进行设置,但未能正确地更改和测量高度.
这是我的代码tableView:heightOfRow:,_samplingView是虚拟视图,它具有工作约束并且与中的相同tableView.
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
{
NSTextField *textField;
NSTextFieldCell *messageCell;
for (NSView *subview in [_samplingView subviews]) {
if ([[subview identifier] isEqualToString:@"message"]) {
textField = (NSTextField*)subview;
messageCell = ((NSTextField*)subview).cell;
}
}
Message *message = [[_messagesArrayController arrangedObjects] objectAtIndex:row];
_samplingView.objectValue = message;
CGFloat width = [[[tableView tableColumns] objectAtIndex:1] width];
[_samplingView setBounds:NSMakeRect(0, 0, width, CGFLOAT_MAX)];
[_samplingView display];
CGFloat optimalHeight = 10 + [messageCell cellSize].height; //messageCell's size …Run Code Online (Sandbox Code Playgroud)