Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fixes a bug affecting resizing of cells with varying and dynamic cell heights. #10

Open
wants to merge 3 commits into from

1 participant

@jivadevoe

This pull request fixes a bug where cells with different heights in the same table would not properly update when the window was resized or scrolled. Additionally, fixes a bug where live resizing would not resize the cells when the table is embedded in an NSSplitView.

@jivadevoe

Added another commit. This is related to the layoutSubviews method that UITableViewCell implements. I've added initial rudimentary support to PXListView for it.

I also removed the calls to PXLog since it was triggering on the DEBUG define which is commonly used by developers but it's symbol was not being found by the linker. So this might cause issues for people who want to use the code with little or no modification.

@jivadevoe

More changes for dragging and dropping. The crash fix is an important one... you're not removing yourself as an observer on NSNotificationCenter, which is causing crashes. At the very least you should definitely take that pull.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 26, 2011
  1. @jivadevoe
Commits on Apr 27, 2011
  1. @jivadevoe

    fixed a drag and drop issue related to focusing another window; added…

    jivadevoe authored
    … layoutSubviews method to PXListViewCell; added reloadRowAtIndex method; removed PXLog log messages
Commits on Apr 28, 2011
  1. @jivadevoe

    fixes a crash when dragging and dropping; fixes cell selection stayin…

    jivadevoe authored
    …g active after dropping a cell"
This page is out of date. Refresh to see the latest.
View
1  Classes/PXListView+Private.h
@@ -38,5 +38,6 @@ typedef NSInteger PXIsDragStartResult;
- (void)enqueueCell:(PXListViewCell*)cell;
- (void)contentViewBoundsDidChange:(NSNotification*)notification;
+-(void)layoutCellsForResizeEvent;
@end
View
43 Classes/PXListView+UserInteraction.m
@@ -211,8 +211,8 @@ - (void)handleMouseDown:(NSEvent*)theEvent inCell:(PXListViewCell*)theCell // Ce
- (void)handleMouseDownOutsideCells: (NSEvent*)theEvent
{
#pragma unused(theEvent)
- [[self window] makeFirstResponder: self];
-
+ //[[self window] makeFirstResponder: self];
+ //
if( _allowsEmptySelection )
[self deselectRows];
else if( _numberOfRows > 1 )
@@ -329,7 +329,6 @@ -(void) setShowsDropHighlight: (BOOL)inState
-(NSUInteger) indexOfRowAtPoint: (NSPoint)pos returningProposedDropHighlight: (PXListViewDropHighlight*)outDropHighlight
{
- PXLog( @"====================" );
*outDropHighlight = PXListViewDropOn;
if( _numberOfRows > 0 )
@@ -339,14 +338,12 @@ -(NSUInteger) indexOfRowAtPoint: (NSPoint)pos returningProposedDropHighlight: (P
{
if( _cellYOffsets[x] > pos.y )
{
- PXLog( @"cellYOffset[%ld] = %f > %f", x, _cellYOffsets[x], pos.y );
break;
}
idx = x;
}
- PXLog( @"idx = %ld", idx );
CGFloat cellHeight = 0,
cellOffset = 0,
@@ -356,8 +353,6 @@ -(NSUInteger) indexOfRowAtPoint: (NSPoint)pos returningProposedDropHighlight: (P
cellOffset = _cellYOffsets[idx];
nextCellOffset = _cellYOffsets[idx+1];
cellHeight = nextCellOffset -cellOffset;
- if( cellHeight < 0 )
- PXLog( @"Urk. (1)" );
}
else if( idx < _numberOfRows && _numberOfRows > 0 ) // drag is somewhere close to or beyond end of list.
{
@@ -365,8 +360,6 @@ -(NSUInteger) indexOfRowAtPoint: (NSPoint)pos returningProposedDropHighlight: (P
cellHeight = [theCell frame].size.height;
cellOffset = [theCell frame].origin.y;
nextCellOffset = cellOffset +cellHeight;
- if( cellHeight < 0 )
- PXLog( @"Urk. (2)" );
}
else if( idx >= _numberOfRows && _numberOfRows > 0 ) // drag is somewhere close to or beyond end of list.
{
@@ -374,24 +367,17 @@ -(NSUInteger) indexOfRowAtPoint: (NSPoint)pos returningProposedDropHighlight: (P
cellOffset = [[self documentView] frame].size.height;
nextCellOffset = cellOffset;
idx = NSUIntegerMax;
- if( cellHeight < 0 )
- PXLog( @"Urk. (3)" );
}
- PXLog( @"cellHeight = %f", cellHeight );
if( pos.y < (cellOffset +(cellHeight / 6.0)) )
{
*outDropHighlight = PXListViewDropAbove;
- PXLog( @"*** ABOVE %ld", idx );
}
else if( pos.y > (nextCellOffset -(cellHeight / 6.0)) )
{
idx++;
*outDropHighlight = PXListViewDropAbove;
- PXLog( @"*** ABOVE %ld (below %d)", idx, idx -1 );
}
- else
- PXLog( @"*** ON %ld", idx );
if( idx > _numberOfRows )
idx = NSUIntegerMax;
@@ -400,7 +386,6 @@ -(NSUInteger) indexOfRowAtPoint: (NSPoint)pos returningProposedDropHighlight: (P
}
else
{
- PXLog( @"*** ON %d", NSUIntegerMax );
return NSUIntegerMax;
}
}
@@ -426,8 +411,6 @@ -(PXListViewCell*) cellForDropHighlight: (PXListViewDropHighlight*)dhl row: (NSU
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
- PXLog( @"draggingEntered" );
-
NSDragOperation theOperation = NSDragOperationNone;
NSUInteger oldDropRow = _dropRow;
@@ -436,14 +419,11 @@ - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
if( [_delegate respondsToSelector: @selector(listView:validateDrop:proposedRow:proposedDropHighlight:)] )
{
NSPoint dragMouse = [[self documentView] convertPoint: [sender draggingLocation] fromView: nil];
- PXLog( @"dragMouse = %@", NSStringFromPoint(dragMouse) );
_dropRow = [self indexOfRowAtPoint: dragMouse returningProposedDropHighlight: &_dropHighlight];
theOperation = [_delegate listView: self validateDrop: sender proposedRow: _dropRow
proposedDropHighlight: _dropHighlight];
}
-
- PXLog( @"op = %lu, row = %ld, hl = %lu", theOperation, _dropRow, _dropHighlight );
if( theOperation != NSDragOperationNone )
{
@@ -456,14 +436,9 @@ - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
[oldCell setDropHighlight: PXListViewDropNowhere];
[newCell setDropHighlight: _dropHighlight];
PXListViewDropHighlight dropHL = ((_dropRow == _numberOfRows) ? PXListViewDropAbove : PXListViewDropOn);
- PXLog( @"TOTAL DROP %s", dropHL == PXListViewDropOn ? "on" : "above" );
[[self documentView] setDropHighlight: dropHL];
}
- else
- PXLog(@"TOTAL DROP unchanged");
}
- else
- PXLog( @"TOTAL DROP NOWHERE" );
return theOperation;
}
@@ -471,8 +446,6 @@ - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender /* if the destination responded to draggingEntered: but not to draggingUpdated: the return value from draggingEntered: is used */
{
- PXLog( @"draggingUpdated" );
-
NSDragOperation theOperation = NSDragOperationNone;
NSUInteger oldDropRow = _dropRow;
@@ -481,15 +454,12 @@ - (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender /* if the destina
if( [_delegate respondsToSelector: @selector(listView:validateDrop:proposedRow:proposedDropHighlight:)] )
{
NSPoint dragMouse = [[self documentView] convertPoint: [sender draggingLocation] fromView: nil];
- PXLog( @"dragMouse = %@", NSStringFromPoint(dragMouse) );
_dropRow = [self indexOfRowAtPoint: dragMouse returningProposedDropHighlight: &_dropHighlight];
theOperation = [_delegate listView: self validateDrop: sender proposedRow: _dropRow
proposedDropHighlight: _dropHighlight];
}
- NSLog( @"op = %lu, row = %ld, hl = %lu", theOperation, _dropRow, _dropHighlight );
-
if( theOperation != NSDragOperationNone )
{
if( oldDropRow != _dropRow
@@ -501,15 +471,11 @@ - (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender /* if the destina
[oldCell setDropHighlight: PXListViewDropNowhere];
[newCell setDropHighlight: _dropHighlight];
PXListViewDropHighlight dropHL = ((_dropRow == _numberOfRows) ? PXListViewDropAbove : PXListViewDropOn);
- NSLog( @"TOTAL DROP %s", dropHL == PXListViewDropOn ? "on" : "above" );
[[self documentView] setDropHighlight: dropHL];
}
- else
- PXLog(@"TOTAL DROP unchanged");
}
else
{
- PXLog( @"TOTAL DROP NOWHERE" );
[self setShowsDropHighlight: NO];
}
@@ -556,6 +522,11 @@ - (void)draggingEnded:(id <NSDraggingInfo>)sender
[oldCell setDropHighlight: PXListViewDropNowhere];
[self setShowsDropHighlight: NO];
+
+ for(PXListViewCell *cell in [self visibleCells])
+ {
+ [cell setDropHighlight:PXListViewDropNowhere];
+ }
}
View
3  Classes/PXListView.h
@@ -60,10 +60,13 @@
@property (nonatomic, assign) BOOL usesLiveResize;
- (void)reloadData;
+-(void)reloadRowAtIndex:(NSInteger)inIndex;
- (PXListViewCell*)dequeueCellWithReusableIdentifier:(NSString*)identifier;
- (NSArray*)visibleCells;
+-(PXListViewCell *)cellForRowAtIndex:(NSUInteger)inIndex;
+
- (NSRange)visibleRange;
- (NSRect)rectOfRow:(NSUInteger)row;
- (void)deselectRows;
View
72 Classes/PXListView.m
@@ -91,9 +91,7 @@ - (void)dealloc
- (void)setDelegate:(id<PXListViewDelegate>)delegate
{
- [_delegate removeObserver:_delegate
- name:PXListViewSelectionDidChange
- object:self];
+ [[NSNotificationCenter defaultCenter] removeObserver:_delegate name:PXListViewSelectionDidChange object:self];
_delegate = delegate;
@@ -105,6 +103,13 @@ - (void)setDelegate:(id<PXListViewDelegate>)delegate
}
}
+-(void)reloadRowAtIndex:(NSInteger)inIndex;
+{
+ [self cacheCellLayout];
+ [self layoutCells];
+ //[self layoutCellsForResizeEvent];
+}
+
- (void)reloadData
{
id <PXListViewDelegate> delegate = [self delegate];
@@ -312,6 +317,11 @@ - (PXListViewCell*)visibleCellForRow:(NSUInteger)row
return outCell;
}
+-(PXListViewCell *)cellForRowAtIndex:(NSUInteger)inIndex
+{
+ return [self visibleCellForRow:inIndex];
+}
+
- (NSArray*)visibleCellsForRowIndexes:(NSIndexSet*)rows
{
NSMutableArray *theCells = [NSMutableArray array];
@@ -495,6 +505,7 @@ - (void)layoutCells
{
NSInteger row = [cell row];
[cell setFrame:[self rectOfRow:row]];
+ [cell layoutSubviews];
}
NSRect bounds = [self bounds];
@@ -596,23 +607,56 @@ - (void)scrollRowToVisible:(NSUInteger)row
- (void)viewWillStartLiveResize
{
_widthPriorToResize = NSWidth([self contentViewRect]);
+ if([self usesLiveResize])
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowSizing:) name:NSSplitViewDidResizeSubviewsNotification object:self.superview];
}
-- (void)viewDidEndLiveResize
+-(void)layoutCellsForResizeEvent
{
- [super viewDidEndLiveResize];
-
- //If we use live resize the view will already be up to date
- if(![self usesLiveResize]) {
- //Change the layout of the cells
- [_visibleCells removeAllObjects];
- [[self documentView] setSubviews:[NSArray array]];
+ //Change the layout of the cells
+ [_visibleCells removeAllObjects];
+ [[self documentView] setSubviews:[NSArray array]];
+
+ [self cacheCellLayout];
+ [self addCellsFromVisibleRange];
+
+ if ([_delegate conformsToProtocol:@protocol(PXListViewDelegate)])
+ {
+ CGFloat totalHeight = 0;
+
+ for (NSUInteger i = 0; i < _numberOfRows; i++)
+ {
+ CGFloat cellHeight = [_delegate listView:self heightOfRow:i];
+ totalHeight += cellHeight +[self cellSpacing];
+ }
- [self cacheCellLayout];
- [self addCellsFromVisibleRange];
+ _totalHeight = totalHeight;
- _currentRange = [self visibleRange];
+ NSRect bounds = [self bounds];
+ CGFloat documentHeight = _totalHeight > NSHeight(bounds) ? _totalHeight:(NSHeight(bounds) - 2);
+
+ [[self documentView] setFrame:NSMakeRect(0.0f, 0.0f, NSWidth([self contentViewRect]), documentHeight)];
}
+
+ _currentRange = [self visibleRange];
+}
+
+-(void)viewDidEndLiveResize
+{
+ [super viewDidEndLiveResize];
+
+ //If we use live resize the view will already be up to date
+ if (![self usesLiveResize])
+ {
+ [self layoutCellsForResizeEvent];
+ }
+ if ([self usesLiveResize])
+ [[NSNotificationCenter defaultCenter] removeObserver:self name:NSSplitViewDidResizeSubviewsNotification object:self.superview];
+}
+
+-(void)windowSizing:(NSNotification *)inNot
+{
+ [self layoutCellsForResizeEvent];
}
#pragma mark -
View
2  Classes/PXListViewCell.h
@@ -35,4 +35,6 @@
- (id)initWithReusableIdentifier:(NSString*)identifier;
- (void)prepareForReuse;
+-(void)layoutSubviews;
+
@end
View
8 Classes/PXListViewCell.m
@@ -144,6 +144,14 @@ - (void)prepareForReuse
_dropHighlight = PXListViewDropNowhere;
}
+
+#pragma mark layout
+
+-(void)layoutSubviews;
+{
+
+}
+
#pragma mark -
#pragma mark Accessibility
Something went wrong with that request. Please try again.