objective-c - 如何在 Objective-C 中将 NSView 转换为 NSTableView

标签 objective-c cocoa

我有一个 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;
}

我想要实现的目标: enter image description here

最佳答案

完全忘记了 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/

相关文章:

objective-c - Objective C Git API 包装器?

objective-c - 核心数据获取仅返回唯一的托管对象

objective-c - 如何在 NSPersistentDocument 和 View Controller 之间共享 NSManagedObjectContext?

cocoa - cocoa 应用程序中的键盘布局更改

ios - 如何使用 NSDateFormatter 生成 2012-07-28T23 :59:00-06:00?

swift - keyDown 不会立即更新 NSTextView

objective-c - 可以检查 Nib 是否已经加载?

ios - 如何使用按钮根据页码播放不同的声音?

ios - 如何在 lldb 中使用枚举

objective-c - 从 Objective C 中的数据库向数组列表收件人发送邮件