2011-06-23 42 views
0

我非常困惑,因为NstableView中的多NsProgressIndicators无法正常工作,addSubview方法总是将进度单元添加到tableview中的最后一行,而不是完全为 行索引我想,这里有一些简单的代码:添加NsProgressIndicator到NstableView中的特殊行

的NSTableView的数据源有两个成员:

@interface tableModel : NSObject { 

NSString *name; 
NsProgressIndicator *progressobj; 
} 

@property(retain) NSString* name; 
@property(retain) NsProgressIndicator *progressobj; 

而且我写了一个自定义单元格类: “ProgressCell”,

Progresscell.m:

- copyWithZone : (NSZone *)zone 
{ 
ProgressCell *cell = (ProgressCell *)[ super copyWithZone:zone ]; 
cell->progress = [ progress retain ]; 
return cell; 
} 

- (void) drawInteriorWithFrame : (NSRect) cellFrame 
        inView: (NSView *) controlView 
{ 
[super drawInteriorWithFrame:cellFrame inView:controlView]; 

if(progress==nil) 
{ 
    return; 
}  
if(![progress superview]) 
{ 
    [controlView addSubview: progress]; 

}  
    [progress setFrame: cellFrame]; 
    [progress sizeToFit]; 
} 

- (void)setProgress:(NsProgressIndicator *)newProgress 
{ 
[newProgress retain]; 
[progress release]; 
progress = newProgress; 
[progress sizeToFit]; 
} 

现在是一些TableModel的对象添加到表视图的数据源的时间:

NSString *[email protected]"test"; 
tableModel *tablemodel=[[tableModel alloc] initWithPriority:[str retain] andProgress:nil]; 
[tableArrays addObject:tablemodel]; 
[tableView reloadData]; 

进度成员是零,因为我不希望显示在开头progressindicator, 想我有一个编辑按钮,如下设置一个动作:

NsProgressIndicator * progress = [[[NsProgressIndicator alloc] init] autorelease]; 
[progress setIndeterminate: NO]; 
[progress setMinValue:0]; 
[progress setMaxValue:100]; 
tableModel * zMyDataObj= [[self tableArrays] objectAtIndex:[tableView selectedRow]]; 
zMyDataObj.progressobj=[progress retain]; 
[tableView reloadData]; 

的willDisplayCell委托方法是:

tableModel * zMyDataObj= [[self tableArrays] objectAtIndex:pRow]; 
if([zMyDataObj progressobj]!=nil) 
{ 
[cell setProgress:[zMyDataObj progressobj]]; 
} 

但是,当我点击编辑按钮,一个新的NsProgressindicator总是添加到最后一行,而不是选定的行,addSubView方法绝对不适合这种情况,所以我想寻求一个最佳的解决方案来设置多亏了

回答

2

cellFrame一个NsProgressIndicator和特殊行之间的连接,是这个问题:

[progress setFrame: cellFrame]; 

cellFrame指的tableView在细胞内的位置,它有一个特定的origin(x,y)。如果你想定位进度指示器,你应该为它创建一个新的矩形,并在cellRect内设置原点。

NSMakeRect((cellFrame.size.width - 20.f)/2, cellFrame.size.height - 20.f), 20.f, 20.f)