NSTableView + NSSegmentedControl + NSSegmentSwitchTrackingSelectAny [英] NSTableView + NSSegmentedControl + NSSegmentSwitchTrackingSelectAny

查看:97
本文介绍了NSTableView + NSSegmentedControl + NSSegmentSwitchTrackingSelectAny的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格视图,其中最右边一列具有NSSegmentedCell。

I've got a table view which has an NSSegmentedCell for its right-most column.

从中提取单元格的NSSegmentedControls使用 any跟踪模式(又名:多重选择),并根据位字段选择分段,其可能值为:1、2和4。值为1表示A | b | c。 2是| B | c。 4是a | b | C。 3是A | B | c,等等...

The NSSegmentedControls which the cells are pulled from are using the "any" tracking mode (aka: multiple selection) and the segments are selected based on a bitfield with possible values: 1, 2, and 4. A value of 1 means A|b|c. 2 is a|B|c. 4 is a|b|C. 3 is A|B|c, etc...

该位字段由数据源的tableView:objectValueForTableColumn:row:方法返回,并且单元格在委托者的tableView:dataCellForTableColumn:row:方法。

That bitfield is returned by the data sources's tableView:objectValueForTableColumn:row: method and the cell is returned in the delegate's tableView:dataCellForTableColumn:row: method.

在tableView:dataCellForTableColumn:row:中单独设置单元格没有任何作用,因为表视图调用tableView:objectValueForTableColumn:row :之后,立即获取该列的值,然后使用它来设置分段控件/单元格的值。

Setting the cells individually in tableView:dataCellForTableColumn:row: doesn't do anything, because the table view calls tableView:objectValueForTableColumn:row: just after that to get the value for that column and then uses it to set the "value" of the segmented control/cell.

问题就在那一点。

似乎位域被误解为段的索引值:A表示0,B表示1,C表示2(以及-1表示无选择) )。...(假定为单项选择。)

It looks like the bitfield is being misinterpreted as the index value of the segment: 0 for A, 1 for B, and 2 for C (and -1 for no selection).... which assumes single selection.

如何进行设置,以便表格视图可以正确设置多个细分? (例如:位域3 = A | B | c)

How do I get this set up so that that the table view will properly set multiple segments? (eg: bitfield 3 = A|B|c)

推荐答案

我是这样做的,它的工作原理是:
1)确保在tableview的数据源/委托方法中引用了分段控制单元;在我的项目中,我只是将分段控制单元与视图控制器类中的插座属性连接起来

2)从表的数据源getter方法返回具有分段控制的列的任何内容(在我的项目中,repeatDays是一个标识符例如,nil

I did it this way, and it works:
1) make sure you have a reference to segmented control cell in your tableview's datasource/delegate methods; in my project, I just connected segmented control cell with outlet property in my view controller class

2) return anything from table's datasource getter method for the column with segmented control (in my project, repeatDays is an identifier of this column), for example, nil

 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)rowIndex {
    NSString *colId = [tableColumn identifier];

    if ([colId isEqualToString:@"repeatDays"]) {
        return nil;
    }

    //handle other columns
}

3)实现tableview的委托willDisplayCell方法

3) implement tableview's delegate willDisplayCell method

- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    NSString *colId = [tableColumn identifier];

    if ([colId isEqualToString:@"repeatDays"]) {
        for (int i = 0; i < self.segmentedCell.segmentCount; ++i) {
            BOOL selected = [[self.days objectForKey:[NSNumber numberWithInt:i]] boolValue];
            [self.segmentedCell setSelected:selected forSegment:i];
        }
    }
}

您需要配置分段单元当表格视图要显示时,请根据您的模型详细信息进行操作,例如,在我的项目中,我有一个可变的字典天,并且它包含与段相对应的布尔值,例如{0:NO,1:YES,2 :否,3:是,4:否,5:否,6:否},其中0、1、2、3、4、5、6-分段,而YES / NO-是否选择该分段


4)最后,实现tableview的数据源setter方法;传递给它的对象是单击段索引;适当修改模型,即单击段索引的翻转标志:

you need to configure segmented cell when table view is about to display it, do it according to your model details, for example, in my project I have a mutable dictionary days, and it contains bool values corresponding to segments, like {0: NO, 1: YES, 2: NO, 3:YES, 4:NO, 5:NO, 6:NO}, where 0, 1, 2, 3, 4, 5, 6 - segments, and YES/NO - whether this segment selected or no

4) the last, implement tableview's datasource setter method; the object passed to it is clicked segment index; modify your model appropriately, that is, flip flag for clicked segment index:

- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    NSString *colId = [tableColumn identifier];

    if ([colId isEqualToString:@"repeatDays"]) {
        int i = [object intValue];
        BOOL selected = [[self.days objectForKey:[NSNumber numberWithInt:i]] boolValue];
        [self.days setObject:[NSNumber numberWithBool:!selected] forKey:[NSNumber numberWithInt:i]];
    }
}

这篇关于NSTableView + NSSegmentedControl + NSSegmentSwitchTrackingSelectAny的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆