开发者

Add a NsProgressIndicator to a special row in NstableView

开发者 https://www.devze.com 2023-03-15 02:48 出处:网络
I\'m very confused because multi NsProgressIndicators in NstableView don\'t work correctly,The addSubview method always add the progress cell to the last row in the tableview,not exactly

I'm very confused because multi NsProgressIndicators in NstableView don't work correctly,The addSubview method always add the progress cell to the last row in the tableview,not exactly the row index i want,here is some simple code:

The NsTableView DataSource have two members:

@interface tableModel : NSObject {

NSString *name;
NsProgressIndicator *开发者_如何学编程progressobj;
}

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

And i write a custom cell class:"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];
}

Now it's the time to add some tableModel objects to the table view datasource:

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

The progress member is nil because i don't want to display a progressindicator at beginning, Guess i have a edit button,set up an action as below:

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];

The willDisplayCell delegate method is:

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

But when i click the edit button, A new NsProgressindicator is always added to the last row,not the selected row,the addSubView method is definitely not suitable for this case,So i want to seek a best solution to set up a connection between a NsProgressIndicator and a special row,thanks


The cellFrame is the problem:

[progress setFrame: cellFrame];

The cellFrame refers to the position of the cell inside the tableView and it has a specific origin(x,y). If you would like to position the progress indicator you should create a new rect for it and set the origin inside the cellRect.

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

精彩评论

暂无评论...
验证码 换一张
取 消