NSTableColumn size to fit contents - objective-c

I am developing in and against Mac OS X 10.6 (Snow Leopard). When I double-click between two of my NSTableView's column headers, the column on the left autosizes, like you would expect.
I want to provide this in a context menu as well, but it seems there is no publicly accessible function to do this. I've Googled, and looked at the documentation for NSTableView, NSTableHeaderView, and NSTableColumn, but found nothing. I find it hard to believe they wouldn't expose something so useful when they obviously have the code written to do it.
I saw the -[NSTableColumn sizeToFit] method, but that only takes the header's size into account. I would also settle for sending the double-click event to the NSTableHeaderView, but couldn't figure out how to do that, either.
Update - I realized it's important to mention I have an NSArrayController (subclass) providing the data to my table, so I don't have an NSTableViewDataSource on which I could call -[tableView: objectValueForTableColumn: row:]. That is the crux of the problem: each column is bound to a keypath of the array controller and that's how it gets its data, so it can't loop through its contents.

You can get the length of each cell by getting the cells from the column.
NSCell myCell = [column dataCellForRow:i];
Then get the width of the cell.
width = [myCell cellSize].width;
After that you can set the width of the cell as the width of the column.
[column setMinWidth:width];
[column setWidth:width];

The following category on NSTableColumn resizes columns to the same width that double-clicking on the separator would produce.
#implementation NSTableColumn (resize)
- (void) resizeToFitContents
{
NSTableView * tableView = self.tableView;
NSRect rect = NSMakeRect(0,0, INFINITY, tableView.rowHeight);
NSInteger columnIndex = [tableView.tableColumns indexOfObject:self];
CGFloat maxSize = 0;
for (NSInteger i = 0; i < tableView.numberOfRows; i++) {
NSCell *cell = [tableView preparedCellAtColumn:columnIndex row:i];
NSSize size = [cell cellSizeForBounds:rect];
maxSize = MAX(maxSize, size.width);
}
self.width = maxSize;
}
#end

For view-based table views (OS X 10.7 and later), you have to use the NSTableView method viewAtColumn:row:makeIfNecessary:, and then get the size.
The following code works if your cell view is an NSTextField. For other views you have to figure out the way to get its minimum size.
[tableView.tableColumns enumerateObjectsUsingBlock:
^(id obj, NSUInteger idx, BOOL *stop) {
NSTableColumn* column = (NSTableColumn*) obj;
CGFloat width = 0;
for (int row = 0; row < tableView.numberOfRows; row++) {
NSView* view = [tableView viewAtColumn: idx
row: row
makeIfNecessary: YES];
NSSize size = [[view cell] cellSize];
width = MAX(width, MIN(column.maxWidth, size.width));
}
column.width = width;
}];
}

I found the following to work for me (written in Swift):
func resizeColumn(columnName: String) {
var longest:CGFloat = 0 //longest cell width
let columnNumber = tableView.columnWithIdentifier(columnName)
let column = tableView.tableColumns[columnNumber] as! NSTableColumn
for (var row = 0; row < tableView.numberOfRows; row++) {
var view = tableView.viewAtColumn(columnNumber, row: row, makeIfNecessary: true) as! NSTableCellView
var width = view.textField!.attributedStringValue.size.width
if (longest < width) {
longest = width
}
}
column.width = longest
viewTable.reloadData()
}
Given a column's identifier as an argument, it finds the cell in that column with the longest content, and resizes the column to fit that content.

I needed to do this very thing and it turned out (for me at least) to be a bit more involved; this isn't perfect but pretty close ;-)
p.s. Edited to handle image based cells
#define GBLBIN(x) [[NSUserDefaults standardUserDefaults] boolForKey:(x)]
- (IBAction)autosizeColumns:(id)sender
{
NSMutableArray * widths = [NSMutableArray array];
NSIndexSet * rows = [table selectedRowIndexes];
NSArray * columns = [table tableColumns];
BOOL deslectAll = NO;
NSNumber * col = nil;
int i, j;
// If no row's selected, then select all, the default
if (![rows count])
{
[table selectAll:sender];
rows = [table selectedRowIndexes];
deslectAll = YES;
}
// Use the selected rows from which we'll use the columns' formatted data widths
for (i=[rows lastIndex]; i != NSNotFound; i=[rows indexLessThanIndex:i])
{
for (j=0; j<[columns count]; j++)
{
NSTableColumn * column = [columns objectAtIndex:j];
id item = [[table dataSource]
tableView:table objectValueForTableColumn:column row:i];
NSCell * cell = [column dataCell];
float width, minWidth=10;
// Depending on the dataCell type (image or text) get the 'width' needed
if ([cell isKindOfClass:[NSTextFieldCell class]])
{
NSFont * font = ([cell font]
? [cell font] : [NSFont controlContentFontOfSize:(-1)]);
NSDictionary * attrs =
[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSFormatter * formatter = [cell formatter];
NSString * string = item;
// We want a string, as IB would have formatted it...
if (![item isKindOfClass:[NSString class]])
if (formatter)
string = [formatter stringForObjectValue:item];
else
string = [NSString stringWithFormat:#"%#", item];
width = [string sizeWithAttributes:attrs].width + minWidth;
}
else
{
// We have NSButtonCell, NSImageCell, etc; get object's width
width = MAX(minWidth,[[cell image] size].width);
}
// First time seeing this column, go with minimum width
if (j == [widths count])
{
[widths addObject:[NSNumber numberWithFloat:minWidth]];
}
col = [widths objectAtIndex:j];
width = MAX([col floatValue], width);
[widths replaceObjectAtIndex:j withObject:[NSNumber numberWithInt:width]];
}
}
// Now go resize each column to its minimum data content width
for (j=0; j<[widths count]; j++)
{
NSTableColumn * column = [columns objectAtIndex:j];
float width = [[widths objectAtIndex:j] floatValue];
if (GBLBIN(#"debug"))
{
NSLog(#"col:%d '%#' %.f", j, [column identifier], width);
}
[column setWidth:width];
}
// Undo select all if we did it
if (deslectAll)
{
[table deselectAll:sender];
}
}

Here are some extension methods to size columns by their content in C# for Monomac developers.
NOTE: this sizes columns by their content only, other considerations no considered (empty cols are 10px wide for eg).
Thanks to slashlos for the obj-c snippet.
public static void SizeColumnsByContent(this NSTableView tableView, int minWidth = 10) {
Contract.Requires(tableView != null);
Contract.Requires(tableView.DataSource != null);
var columns = tableView.TableColumns();
var widths = new List<int>();
for (int row=0; row< tableView.RowCount; row++) {
for (int col=0; col < tableView.ColumnCount; col++) {
// Determine what the fit width is for this cell
var column = columns[col];
var objectValue = tableView.DataSource.GetObjectValue(tableView, column, row);
var width = column.DataCell.DetermineFitWidth(objectValue, minWidth);
// Record the max width encountered for current coolumn
if (row == 0) {
widths.Add(width);
} else {
widths[col] = Math.Max(widths[col], width);
}
}
// Now go resize each column to its minimum data content width
for (int col=0; col < tableView.ColumnCount; col++) {
columns[col].Width = widths[col];
}
}
}
public static int DetermineFitWidth(this NSCell cell, NSObject objectValueAtCell, int minWidth = 10) {
int width;
if (cell is NSTextFieldCell) {
var font = cell.Font ?? NSFont.ControlContentFontOfSize(-1);
var attrs = NSDictionary.FromObjectAndKey(font, NSAttributedString.FontAttributeName);
// Determine the text on the cell
NSString cellText;
if (objectValueAtCell is NSString) {
cellText = (NSString)objectValueAtCell;
} else if (cell.Formatter != null) {
cellText = cell.Formatter.StringFor(objectValueAtCell).ToNSString();
} else {
cellText = objectValueAtCell.Description.ToNSString();
}
width = (int)cellText.StringSize(attrs).Width + minWidth;
} else if (cell.Image != null) {
// if cell has an image, use that images width
width = (int)Math.Max(minWidth, (int)cell.Image.Size.Width);
} else {
// cell is something else, just use its width
width = (int)Math.Max(minWidth, (int)cell.CellSize.Width);
}
return width;
}

I had trouble implementing some of the other solutions... But I managed to get columns to automatically resize width when cells are created using the cell's string value. Seems to work pretty well so far!
- (id)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSString *identifier = [tableColumn identifier];
NSUInteger col = [keys indexOfObject:identifier];
// Get an existing cell with the MyView identifier if it exists
NSTextField *cell = [tableView makeViewWithIdentifier:identifier owner:self];
// Get cell data
NSDictionary *dict = [self.tableContents objectAtIndex:row];
NSString *stringValue = [dict objectForKey:[keys objectAtIndex:col]];
// There is no existing cell to reuse so create a new one
if (cell == nil) {
// Create the new NSTextField with a frame of the {0,0} with the width of the table.
// Note that the height of the frame is not really relevant, because the row height will modify the height.
NSRect rect = NSRectFromCGRect(CGRectMake(0, 0, 64, 24));
cell = [[NSTextField alloc] initWithFrame:rect];
[cell setBordered:NO];
[cell setBackgroundColor:[NSColor clearColor]];
[cell setAutoresizesSubviews:YES];
// autosize to fit width - update column min width
NSRect rectAutoWidth = [stringValue boundingRectWithSize:(CGSize){CGFLOAT_MAX, 24} options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:cell.font}];
if ( tableColumn.minWidth < rectAutoWidth.size.width )
{
[tableColumn setMinWidth:rectAutoWidth.size.width+20]; // add padding
}
// The identifier of the NSTextField instance is set to MyView. This allows the cell to be reused.
cell.identifier = identifier;
}
// result is now guaranteed to be valid, either as a reused cell or as a new cell, so set the stringValue of the cell to the nameArray value at row
// set value
cell.stringValue = stringValue;
// Return the result
return cell;
}

Related

Warning in use custom collectionViewFlowLayot

I just modify the attributes in my flowLayout
-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSMutableArray *array = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
for (int i = 0; i< array.count; i++) {
UICollectionViewLayoutAttributes* attributes = array[i];
if (!attributes.representedElementKind) {
array[i] = [self layoutAttributesForItemAtIndexPath:attributes.indexPath];
}
}
return array;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForItemAtIndexPath:indexPath];
CGFloat midX = self.collectionView.contentOffset.x + self.collectionView.width * 0.5;
CGFloat distance = midX - attributes.center.x;
CGFloat activeDistance = self.collectionView.width - lineSpacing - remainSpacing * 2;
CGFloat normalizedDistance = distance / activeDistance;
attributes.alpha = 1 - (1 - alphaFactor) * ABS(normalizedDistance);
CGFloat zoom = 1 - (1 - scaleFactor) * ABS(normalizedDistance);
attributes.transform3D = CATransform3DMakeScale(1.0, zoom, 1.0);
attributes.zIndex = 1;
return attributes;
}
Question like this, console logout in the first time of change the showed cell:
UICollectionViewFlowLayout has cached frame mismatch for index path {length = 2, path = 0 - 1} - cached value: {{350, 15.75}, {265, 283.5}}; expected value: {{350, 0}, {265, 315}}
This is likely occurring because the flow layout subclass MyFlowLayout is modifying attributes returned by UICollectionViewFlowLayout without copying them
Do like this, to copy the attributes
UICollectionViewLayoutAttributes *attributes = [[super layoutAttributesForItemAtIndexPath:indexPath] copy];
This question has solved in this issue:
Warning: UICollectionViewFlowLayout has cached frame mismatch for index path 'abc'

Filling a grid with buttons

I am new to objective c and trying the learn the basics of creating a UI. In my UIView class, I have created a grid along with buttons, yet those buttons don't actually exist (as far as I can tell). Ideally, when I click a button, the image is supposed to change, but that doesn't happen. Where should I be looking at to fix?
- (id)initWithFrame:(CGRect)frame {
if( self = [super init]){
tiles_ = [NSMutableArray array];
tileClosed = NO;
}
return self = [super initWithFrame:frame];
}
- (void) initTile : (Tile *) sender
{
int MINE_COUNT = 16;
for(int i = 0; i < MINE_COUNT; i++){
while(1){
int rand = random() % [tiles_ count];
Tile * tile = [tiles_ objectAtIndex:rand];
if(tile != sender && !tile.isMine){
tile.isMine = YES;
break;
}
}
}
tileClosed = YES;
}
- (void)drawRect:(CGRect)rect
{
NSLog( #"drawRect:" );
CGContextRef context = UIGraphicsGetCurrentContext();
// shrink into upper left quadrant
CGRect bounds = [self bounds]; // get view's location and size
CGFloat w = CGRectGetWidth( bounds ); // w = width of view (in points)
CGFloat h = CGRectGetHeight ( bounds ); // h = height of view (in points)
dw = w/16.0f; // dw = width of cell (in points)
dh = h/16.0f; // dh = height of cell (in points)
NSLog( #"view (width,height) = (%g,%g)", w, h );
NSLog( #"cell (width,height) = (%g,%g)", dw, dh );
// draw lines to form a 16x16 cell grid
CGContextBeginPath( context ); // begin collecting drawing operations
for ( int i = 1; i < 16; ++i )
{
// draw horizontal grid line
CGContextMoveToPoint( context, 0, i*dh );
CGContextAddLineToPoint( context, w, i*dh );
}
for ( int i = 1; i < 16; ++i )
{
// draw vertical grid line
CGContextMoveToPoint( context, i*dw, 0 );
CGContextAddLineToPoint( context, i*dw, h );
}
for(int x=1; x<16;x++){
for(int y=1;y<16;y++){
Tile * tile = [[Tile alloc] init];
[tile setFrame:CGRectMake(x * 16.0f, y * 16.0f, 16.0f, 16.0f)];
[tile addTarget:self action:#selector(clickCell:) forControlEvents:UIControlEventTouchUpInside];
[tiles_ addObject: tile]; }
}
[[UIColor grayColor] setStroke]; // use gray as stroke color
CGContextDrawPath( context, kCGPathStroke ); // execute collected drawing ops
}
- (void) clickCell : (Tile *) sender
{
if(! tileClosed) [self initTile: sender];
[sender open];
}
Your initwithFrame: method is broken: the return line should just be return self;.
What you're doing at the moment in effect clobbers the tiles_ array, so it ends up as nil, hence attempting to store tiles in it does nothing (and hence they aren't retained).
Your init method is incorrect and you can change it simply to
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame]
if( self)
{
tiles_ = [NSMutableArray array];
tileClosed = NO;
}
return self;
}
and for the buttons to be appearing you are not adding it to the subview the
[self.view addSubview:tile];
is missing.

Scrolling images by dragging UISlider

Iam beginner in IOS Dev. asking about a way to make UIScrollView images scrolls by dragging UISlider . This is my case :
Required case :
when dragging slider , images scrolls on UIScrollView . also when scrolling images , slider changes its value accordingly .
Actual case :
when scrolling images , slider changes its value accordingly BUT when dragging slider images DONOT Scroll .
Here is my code , I wish any one tell me how to scrolling when slider drags .
Slider IBAction
- (IBAction)sliding:(UISlider *)sender{
int slider_value = (int)slider.value;
NSString *current_page = [[NSString alloc] initWithFormat:#"%i",slider_value];
current_page_lbl.text = current_page ;
[self loadPage:slider_value];
[self loadVisiblePages];
}
methods for UIScrollview :
- (void)loadPage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
return;
}
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView == [NSNull null]) {
CGRect frame = self.scrollView.bounds;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0.0f;
UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]];
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
}
}
- (void)purgePage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what you have to display, then do nothing
return;
}
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView != [NSNull null]) {
[pageView removeFromSuperview];
[self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
To loadVisiblePage
- (void)loadVisiblePages {
CGFloat pageWidth = self.scrollView.frame.size.width;
NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
NSLog(#"page loaded is %d",page);
self.pageControl.currentPage = page;
NSInteger firstPage = page - 1;
NSInteger lastPage = page + 1;
for (NSInteger i=0; i<firstPage; i++) {
[self purgePage:i];
}
for (NSInteger i=firstPage; i<=lastPage; i++) {
[self loadPage:i];
slider.value = 21-i ;
int slider_value = (int)slider.value;
NSString *current_page = [[NSString alloc] initWithFormat:#"%i",21-slider_value];
current_page_lbl.text = current_page ;
}
for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) {
[self purgePage:i];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self loadVisiblePages];
}
uislider is not suitable to use with uiscrollview....for this purpose there is one controll available name UIPagecontrol..
following link is the nice example with code which direct you to how to use it with UIScollview
UIPagecontroll with UIscollview with code Click Here

NSMutableArray in Class Method

I've created a class method that returns a random position as an NSValue object.
I want to store the returned positions in a mutable array so the next time the class method gets called it checks the positions in the mutable array and if it finds one that is too close to the one stored it generates a new one.
What I can't seem to figure out is how to allocate and initialize my mutable array to use with my class method. I've tried allocating it in the class method. but that won't work because it will get initialized on each call therefor the previously stored values are... erased?
then I want to use the generated position to place a number of circles on the view (without them overlapping). right now the randomly generated positions work, but they overlap because of the mutable array not being implemented correctly in my randomposition class.
I'm still a beginner in objective c...and programming in general...
#implementation randomPosition
NSMutableArray *positionsArrayPlayer1;
+(NSValue *) givePosition:(int)player forRadius: (CGFloat) radius
{
if (player == 1)
{
//set range for arc4random
int fromNumberY = 550;
int toNumberY = 950;
//set range for arc4random
int fromNumberX = 0;
int toNumberX = 700;
BOOL far_enough_away = NO;
CGPoint newpoint;
while(!far_enough_away)
{
newpoint = CGPointMake((arc4random()%(toNumberX-fromNumberX+1))+fromNumberX,
(arc4random()%(toNumberY-fromNumberY+1))+fromNumberY);
far_enough_away = YES;
for(NSValue *existing in positionsArrayPlayer1)
{
NSLog(#"test");
CGPoint pointb = [existing CGPointValue];
CGFloat deltay = pointb.y-newpoint.y;
CGFloat deltax = pointb.x-newpoint.x;
CGFloat distance = sqrt(pow(deltax,2) + pow(deltay,2));
//fail if closer than desired radius
if(distance < radius )
{
far_enough_away = NO;
NSLog(#"test");
break;
}
[positionsArrayPlayer1 addObject:[NSValue valueWithCGPoint:newpoint]];
}
NSLog(#"%#",positionsArrayPlayer1);
}
return [NSValue valueWithCGPoint:newpoint];
} else if (player == 2 ){
//more stuff to come here....
} else {
NSLog(#"player invalid");
}
return nil;
}
here's the method I use in my viewcontroller. I also store the circle object that is placed on the view in a separate array for other manipulations.
- (void) positionCirclesPlayer1
{
radius = 70;
CGRect position;
for (int i = 0; i < [colors count];i++)
{
CGRect positionCircleInCenter = CGRectMake(self.view.frame.size.width/2-35, self.view.frame.size.height/2-35, radius, radius);
Circle *myCircle = [[Circle alloc] initWithFrame:positionCircleInCenter radius:radius color:[colors objectAtIndex:i]];
myCircle.label.text = [NSString stringWithFormat:#"%i", i];
myCircle.alpha = 0;
myCircle.userInteractionEnabled = NO;
theNewPointPlayer1 = [randomPosition givePosition:1 forRadius:radius];
position = CGRectMake(theNewPointPlayer1.CGPointValue.x, theNewPointPlayer1.CGPointValue.y, 70, 70);
[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationCurveEaseIn animations:^{
myCircle.frame = position; myCircle.alpha = 1;} completion:nil];
[playerOneCircles addObject:myCircle];
[self.view addSubview:myCircle];
}
}
Before learning Obj-C, you should probably learn something about C :)
+(NSValue*)givePosition:(int)player forRadius:(CGFloat)radius {
//this is executed when the method is called for the first time
static NSMutableArray *positionsArrayPlayer1 = nil;
if (positionsArrayPlayer1 == nil) { //checking if this is the first time execution
positionsArrayPlayer1 = [[NSMutableArray alloc] init];
}
}
or
//global variable accessible only from this file
static NSMutableArray *positionsArrayPlayer1;
#implementation randomPosition
//this method is called when the class is loaded.
+(void)initialize {
positionsArrayPlayer1 = [[NSMutableArray alloc] init];
}
+(NSValue*)givePosition:(int)player forRadius:(CGFloat)radius {
//do something with the array
}
#end
Note that implementing this functionality using class methods is probably not a very good architecture.

iOS: Multiple intersecting views

I am making a day view calendar just like the native iPhone calendar. I am trying to position the tiles the same as in the native calendar, side by side, if they are the same size and same time.
However, I can only figure out how to do it to 2 tiles and not multiple tiles. In the attached image I have 4 tiles. One that expands slightly into the other 3. I then have the first tile on the far left and the second tile just after the first one. Now I need to figure out how to add the additional tiles?
How would I do this for more than 2 tiles?
About the image: If you can't see it the 3rd tile is ontop of the 2nd tile (you can see it is a bit darker since they are on top of each other.
- (void)layoutSubviews
{
// Set the main
for (UIView *view in self.subviews) {
APCalendarDayTile *tile = (APCalendarDayTile *)view;
CGFloat startPos = [APCalendarCurrentDayView yAxisForTime:[APCalendarCurrentDayView minutesToTime:tile.appointment.startDate]];
CGFloat endPos = [APCalendarCurrentDayView yAxisForTime:[APCalendarCurrentDayView minutesToTime:tile.appointment.endDate]];
tile.frame = CGRectMake(kLeftSideBuffer, startPos, (self.bounds.size.width - kLeftSideBuffer) , endPos - startPos);
tile.backgroundColor = [UIColor colorWithHexString:tile.appointment.appointmentColor];
}
for (UIView *view in self.subviews) {
APCalendarDayTile *tile = (APCalendarDayTile *)view;
if ([self viewIntersectsWithAnotherView:tile]) {
}
}
}
- (BOOL)viewIntersectsWithAnotherView:(UIView*)selectedView{
NSArray *subViewsInView=[self subviews];// I assume self is a subclass
// of UIViewController but the view can be
//any UIView that'd act as a container
//for all other views.
for (UIView *theView in subViewsInView){
if (![selectedView isEqual:theView]) {
if(CGRectIntersectsRect(selectedView.frame, theView.frame)) {
if ((selectedView.frame.origin.y == theView.frame.origin.y) && (selectedView.frame.size.height == theView.frame.size.height)) {
if (theView.frame.size.width == self.bounds.size.width - kLeftSideBuffer) {
theView.frame = CGRectMake(theView.frame.origin.x, selectedView.frame.origin.y, theView.frame.size.width / 2, selectedView.frame.size.height);
}
selectedView.frame = CGRectMake(theView.frame.origin.x + theView.frame.size.width, selectedView.frame.origin.y, theView.frame.size.width, selectedView.frame.size.height);
return YES;
}
}
}
}
return NO;
}
It appears that your test
if ((selectedView.frame.origin.y == theView.frame.origin.y) && (selectedView.frame.size.height == theView.frame.size.height))
Is only applied to views of equal y origin and height. I would solve this problem using the following pseudo code:
initialize an empty arranged subviews array
initialize a nil previous subview
for every subview
if the subview intersects with the previous subview
ensure the subview and the previous subview are added to the arranged subviews array
else if the arranged subviews array is not empty
arrange the subviews in the array across the width of their superview
empty the arranged subview array
Ok,
I sorta took SaltyMule's approach however, his pseudo code didn't make sense in the if / else.
- (void)layoutSubviews
{
// Set the main
for (UIView *view in self.subviews) {
APCalendarDayTile *tile = (APCalendarDayTile *)view;
CGFloat startPos = [APCalendarCurrentDayView yAxisForTime:[APCalendarCurrentDayView minutesToTime:tile.appointment.startDate]];
CGFloat endPos = [APCalendarCurrentDayView yAxisForTime:[APCalendarCurrentDayView minutesToTime:tile.appointment.endDate]];
tile.frame = CGRectMake(kLeftSideBuffer, startPos, (self.bounds.size.width - kLeftSideBuffer) , endPos - startPos);
tile.backgroundColor = [UIColor colorWithHexString:tile.appointment.appointmentColor];
}
[sameTimeAppointments removeAllObjects];
for (UIView *view in self.subviews) {
APCalendarDayTile *tile = (APCalendarDayTile *)view;
if ([self viewIntersectsWithAnotherView:tile]) {
if ([sameTimeAppointments objectForKey:[NSString stringWithFormat:#"%f", tile.frame.origin.y]] != nil) {
NSMutableArray *tempArray = [[sameTimeAppointments objectForKey:[NSString stringWithFormat:#"%f", tile.frame.origin.y]] mutableCopy];
[tempArray addObject:tile];
[sameTimeAppointments setValue:tempArray forKey:[NSString stringWithFormat:#"%f", tile.frame.origin.y]];
} else {
[sameTimeAppointments setValue:[NSMutableArray arrayWithObject:tile] forKey:[NSString stringWithFormat:#"%f", tile.frame.origin.y]];
}
}
}
for (NSString *currentDict in sameTimeAppointments) {
NSArray *currentAppointments = [sameTimeAppointments objectForKey:currentDict];
float tileWidth = ((self.frame.size.width - kLeftSideBuffer) / [currentAppointments count]);
for (int i = 0; i < [currentAppointments count]; i++) {
APCalendarDayTile *tile = [currentAppointments objectAtIndex:i];
float xPos = 0.0 + kLeftSideBuffer;
if (i != 0) {
xPos = (((APCalendarDayTile *)[currentAppointments objectAtIndex:i - 1]).frame.origin.x + tileWidth);
}
tile.frame = CGRectMake(xPos, tile.frame.origin.y, tileWidth, tile.frame.size.height);
[self bringSubviewToFront:tile];
}
}
}
- (BOOL)viewIntersectsWithAnotherView:(UIView*)selectedView{
NSArray *subViewsInView=[self subviews];// I assume self is a subclass
// of UIViewController but the view can be
//any UIView that'd act as a container
//for all other views.
for (UIView *theView in subViewsInView){
if (![selectedView isEqual:theView]) {
if(CGRectIntersectsRect(selectedView.frame, theView.frame)) {
if ((selectedView.frame.origin.y == theView.frame.origin.y) && (selectedView.frame.size.height == theView.frame.size.height)) {
return YES;
}
}
}
}
return NO;
}