我有一个 NSTableView,它有多个自定义 NSTableViewCell 对象。 每个 NSTableViewCell 对象都有三个 NSTextField。 我想发送每个 NSField 中输入的文本并将其分配给一个字符串变量。 问题是我需要获取 super View 对象才能确定其中存在该自定义 NSTableViewCell 的 selectedRow。
在 Swift 中可能看起来像这样:
let textField = sender as! NStextField
let myView = textField.superview?.superview?.superview as! NSTableView
let num = myView.row(for: textField.superview!)
我对 NSTextField 之一尝试过此操作:
- (IBAction)setValue:(id)sender {
NSTableView *thisTable = self.cell.superview.superview.superview.superview;
NSInteger num = [thisTable selectedRow];
}
它不起作用,而且我还收到此警告:
Incompatible pointer types initializing 'NSTableView *' with an expression of type 'NSView * _Nullable'
我的 CustomCell 类是一个简单的类,仅用于绘制单元格。
@implementation CustomCell
- (id)init {
self = [super init];
if(self != nil) {
_cellTitle;
_cellBackLight;
_cellTime;
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
}
.h 文件:
@interface CustomCell : NSTableCellView
@property NSString *cellTitle;
@property double cellValue;
@property int cellTime;
@property (strong) IBOutlet NSTextField *titleField;
@property (strong) IBOutlet NSTextField *valueField;
@property (strong) IBOutlet NSTextField *durationField;
//All outlets are linked from Storyboard to the CustomCell class.
@end
tableView 委托(delegate):
- (id)tableView:(NSTableView *)viewTableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)rowIndex {
NSTableCellView *cell = [viewTableView makeViewWithIdentifier:tableColumn.identifier owner:self];
cell = [viewTableView makeViewWithIdentifier:@"MainCell" owner:self];
return cell;
}
最佳答案
完全忘记了 Objective C
的语法,但以下是如何在 iOS 版 Swift
中实现这一点。
免责声明:我很清楚 iOS 和 MacOS 开发之间存在许多差异,但是这是“如何组织代码来实现此特定任务”的展示。即使它不可复制/粘贴,甚至未经测试,所采用的方法仍然相同。
假设你有你的类 Person
:
class Person {
var title: String = ""
var name: String = ""
var surname: String = ""
}
您可以在 ViewController 中添加一个 Person
数组。
该数组绑定(bind)到您的 tableView 数据源,因此其中的 Person
实例将显示在您的 TableView
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var personArray: [Person] = []
// MARK:- TableViewDelegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return personArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath) as! CustomCell
cell.person = personArray[indexPath.row] // - 1
return cell
}
}
正如您所注意到的,单元格现在包含一个新的引用person
,指向您要显示/编辑的关联人员。
请参阅下面的 CustomCell
实现:
class CustomCell: UITableViewCell, UITextFieldDelegate {
weak var person: Person? { // the reference to the associated person
didSet {
// when Person is set, we update the fields
titleTextField.text = person?.title ?? ""
nameTextField.text = person?.name ?? ""
surnameTextField.text = person?.surname ?? ""
}
}
@IBOutlet weak var titleTextField: UITextField!
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var surnameTextField: UITextField!
override func awakeFromNib() {
super.awakeFromNib()
// Add action when textField text change
titleTextField.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
nameTextField.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
surnameTextField.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
}
@objc func textFieldDidChange(textField: UITextField){
// switch on the textField to see which info should be edited
switch textField {
case titleTextField:
person?.title = titleTextField.text ?? ""
case nameTextField:
person?.name = nameTextField.text ?? ""
case surnameTextField:
person?.surname = surnameTextField.text ?? ""
default: break
}
}
}
这样做时,您对 Person
的修改将在关联的 CustomCell
中处理。
当表格 View 重新加载时,它将保留您已经输入的数据
当您编辑 TextField 时,它会更改 person
并考虑到它是一个引用,您将在主 VC 上的 personArray
中看到差异
再次强调,它不可复制/粘贴,应被视为伪代码
关于objective-c - 如何在 Objective-C 中将 NSView 转换为 NSTableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56486207/