Created
March 16, 2011 22:52
-
-
Save xslim/873494 to your computer and use it in GitHub Desktop.
Get max mini-cell width
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Beta !! | |
- (NSArray *)widthForData:(NSArray *)dataArray | |
{ | |
if ([dataArray count] < 1) return nil; | |
CGFloat totalWidth = 0.0f; | |
UIFont *font = [UIFont systemFontOfSize:12.0f]; | |
float maxWidth = 400.0f; | |
float minWidth = 20.0f; | |
int sCount = [[dataArray objectAtIndex:0] count]; | |
float sSizes[sCount]; | |
for (NSArray *stringArray in dataArray) { | |
int idx = 0; | |
for (NSString *s in stringArray) { | |
CGSize sSize = [s sizeWithFont:font constrainedToSize:CGSizeMake(maxWidth, 20.0f) lineBreakMode:UILineBreakModeTailTruncation]; | |
float sWidth = sSize.width + 20.0f; | |
if (sWidth > sSizes[idx]) { | |
sSizes[idx] = (sWidth < maxWidth) ? (sWidth > minWidth) ? sWidth : minWidth : maxWidth; | |
} | |
idx++; | |
} | |
} | |
NSMutableArray *mA = [NSMutableArray arrayWithCapacity:sCount]; | |
for (int i=0; i<sCount; i++) { | |
totalWidth += sSizes[i]; | |
[mA addObject:[NSNumber numberWithFloat:sSizes[i]]]; | |
} | |
//hack | |
CGRect tableFrame = self.tableView.frame; | |
tableFrame.size.width = (totalWidth + 30.0f); | |
self.tableView.frame = tableFrame; | |
return mA; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment