When I load UITabelView on viewDidLoad it does not show proper data in cells, but when I scroll the tabelview it sets proper data. What's wrong in my code?
Here the code I use.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [_AnswerKeyTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
for(id currentObject in topLevelObject)
{
if([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (CustomCell*)currentObject;
}
}
}
ModelInfo *info = [mArray objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(212, 20000.0f);
NSString *temp=[NSString stringWithFormat:#"%# (%#)",info.your_answer,info.answer_status];
CGSize question_size = [info.question sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGSize your_answer_size = [temp sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGSize correct_answer_size = [info.correct_answer sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
NSString * ans_string;
if([info.answer_status isEqualToString:#"Correct answered"])
{
ans_string = [NSString stringWithFormat:#"%#(<font color=\"#7EBA00\"><b>%#</b></font>)",info.your_answer,info.answer_status];
cell.lblCorrectAnswer.hidden = YES;
cell.lblC_Ans.hidden = YES;
}else if([info.answer_status isEqualToString:#"Not answered"]){
ans_string = [NSString stringWithFormat:#"<font color=\"#FF0000\"><b>%#</b></font>)",info.answer_status];
cell.lblCorrectAnswer.hidden = NO;
cell.lblC_Ans.hidden = NO;
cell.lblCorrectAnswer.text =info.correct_answer;
[cell.lblCorrectAnswer setFrame:CGRectMake(103,your_answer_size.height+question_size.height+9,212,correct_answer_size.height)];
[cell.lblC_Ans setFrame:CGRectMake(cell.lblC_Ans.frame.origin.x,your_answer_size.height+question_size.height+9,cell.lblC_Ans.frame.size.width,21)];
}else{
ans_string = [NSString stringWithFormat:#"%#(<font color=\"#FF0000\"><b>%#</b></font>)",info.your_answer,info.answer_status];
cell.lblCorrectAnswer.hidden = NO;
cell.lblC_Ans.hidden = NO;
cell.lblCorrectAnswer.text =info.correct_answer;
[cell.lblCorrectAnswer setFrame:CGRectMake(103,your_answer_size.height+question_size.height+9,212,correct_answer_size.height)];
[cell.lblC_Ans setFrame:CGRectMake(cell.lblC_Ans.frame.origin.x,your_answer_size.height+question_size.height+9,cell.lblC_Ans.frame.size.width,21)];
}
cell.lblQuestion.text = info.question;
cell.lblYourAnswer.text = ans_string;
cell.lblYourAnswer.attributedText=[OHASBasicHTMLParser attributedStringByProcessingMarkupInAttributedString:cell.lblYourAnswer.attributedText];
cell.lblQue.text = [NSString stringWithFormat:#"Question %d",indexPath.row+1];
[cell.lblQuestion setFrame:CGRectMake(103, 3,212,question_size.height)];
[cell.lblYourAnswer setFrame:CGRectMake(103,cell.lblQuestion.frame.size.height+6,212,your_answer_size.height)];
[cell.lblAns setFrame:CGRectMake(cell.lblAns.frame.origin.x,cell.lblQuestion.frame.size.height+6,cell.lblAns.frame.size.width,21)];
[cell.view setFrame:CGRectMake(5,1,310,cell.lblQuestion.frame.size.height+cell.lblYourAnswer.frame.size.height+cell.lblCorrectAnswer.frame.size.height)];
[cell setFrame:CGRectMake(0,0,320,cell.view.frame.size.height)];
[cell setFrame:CGRectMake(0,0,320,cell.frame.size.height+10)];
return cell;
}
You can't set the frame of a cell -tableView:cellForRowAtIndexPath:. That is handled automatically by the table view.
To tell the table view the height needed for a cell, use -tableView:heightForRowAtIndexPath:.
Related
i have created a custom cell with a textview. I need to expand the height of the textview and the height of the custom cell according to the content in the textview without using auto layout. can any one help me
This is the code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return sendername.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"MenuCtrl";
tsecureMsgConCell *cell = [tabVieConv dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MsgConCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.userName.text = [sendername objectAtIndex:indexPath.row];
cell.msgContentTxView.text = [msgContents objectAtIndex:indexPath.row];
NSString * sentDateCell = [msgSenddate objectAtIndex:indexPath.row];
NSString * replyDateCell = [msgRevidate objectAtIndex:indexPath.row];
thredIdCell = [TSecureMsgId objectAtIndex:indexPath.row];
if ([sentDateCell isEqualToString:#"NULL"])
{
cell.dateLbl.text = [msgRevidate objectAtIndex:indexPath.row];
cell.timeLbl.text = [msgRecTime objectAtIndex:indexPath.row];
cell.imgVew.image = [UIImage imageNamed:#"recive.png"];
}
else if ([replyDateCell isEqualToString:#"NULL"])
{
cell.dateLbl.text = [msgSenddate objectAtIndex:indexPath.row];
cell.timeLbl.text = [msgSendTime objectAtIndex:indexPath.row];
cell.imgVew.image = [UIImage imageNamed:#"sent.png"];
}
return cell;
}
msgContentTxView - is my textview
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = indexPath.row;
if (row != NSNotFound)
{
if ([exapmlestring length] > 0)
{
CGFloat padding = 17.0f;
CGFloat labelWidth = self.tableview.bounds.size.width - padding*2;
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"HelveticaNeue" size:17.0], NSFontAttributeName,
nil];
NSAttributedString *text = [[NSAttributedString alloc] initWithString:exapmlestring attributes:attributesDictionary];
NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin;
///size
CGRect boundingRect = [text boundingRectWithSize:CGSizeMake(labelWidth, CGFLOAT_MAX)
options:options
context:nil];
boundingRect.size.height = boundingRect.size.height + 100;
return (CGFloat) (ceil(boundingRect.size.height) + padding*2);
}
}
return 0;
}
this is my code that shows data in table,my complete data is not showing in cell.
1st image is when i set scrollable to no ,2nd image when i do not set scrollable.i am a beginner.plz help me out of this.
- (void)textViewDidChange:(UITextView *)textView{
[table beginUpdates];
[table endUpdates];
}
-(void)createdatabase{
BOOL success;
NSFileManager *filemanager = [NSFileManager defaultManager];
success = [filemanager fileExistsAtPath:datapath];
if (success)return;
NSString *databasefromapp = [[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:dataname];
[filemanager copyItemAtPath:databasefromapp toPath:datapath error:nil];
}
-(void)getdatabase{
eventitleary = [[NSMutableArray alloc]init];
eventdescary = [[NSMutableArray alloc]init];
eventimgary = [[NSMutableArray alloc] init];
sqlite3 *dataname1;
if (sqlite3_open([datapath UTF8String],&dataname1) == SQLITE_OK) {
const char *sqlStatement;
sqlStatement = "SELECT * FROM photography_events";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(dataname1, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
// Read the data from the result row
NSString *str_title = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
[eventitleary addObject:str_title];
NSString *str_desc = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
[eventdescary addObject:str_desc];
NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 2) length:sqlite3_column_bytes(compiledStatement, 2)];
[eventimgary addObject:data];
}
}
}
NSLog(#"%#", eventitleary);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return eventitleary.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellidenti = #"CellIdenti";
TableViewCell2 *cell = (TableViewCell2*)[tableView dequeueReusableCellWithIdentifier:cellidenti];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:#"TableViewCell2" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.eventitlelbl.text = [eventitleary objectAtIndex:indexPath.row];
cell.eventdesc.text = [eventdescary objectAtIndex:indexPath.row];
cell.eventdesc.editable = NO;
//cell.eventdesc.scrollEnabled = NO;
[cell.eventdesc sizeToFit];
frame = cell.eventdesc.frame;
frame.size = cell.eventdesc.contentSize;
cell.eventdesc.frame = frame;
NSData *dataimg = (NSData*)[eventimgary objectAtIndex:indexPath.row];
cell.eventimg.image = [UIImage imageWithData:dataimg];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// Return the height with a bit of additional padding space
return frame.size.height + 300;
}
You can achieve this by adaptive layout. Check this awesome tutorial if you are working with autolayout. Else you can set dynamic tableView cell height by calculating the height in which your text will fit.
You can calculate height of text by using below method. Pass text, required font and width of your textview.
-(CGFloat)heightForText:(NSString*)text withFont:(UIFont *)font andWidth:(CGFloat)width
{
CGSize constrainedSize = CGSizeMake(width, MAXFLOAT);
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text attributes:attributesDictionary];
CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
if (requiredHeight.size.width > width) {
requiredHeight = CGRectMake(0,0,width, requiredHeight.size.height);
}
return requiredHeight.size.height;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return [self heightForText:#"your text view text for this row" withFont:[UIFont fontWithName:#"Helvetica" size:16] andWidth:320];
}
i just got it, i wanted to do it dynamically rather than using auto layout,here is my code.Hope in future it works out to anyone.
Currently I'm making a Messaging App but recently the tableCells are not resizing as expected. I've used the new iOS 8 relative height functionality but still nothing changes. All the design it's being done via Storyboard so far everything works as expected but need to tell the tableCell to resize based on the textview height. here how it's currently looking.
http://i.imgur.com/lqoxxJV.png
The Code I'm using is the following.
- (UITableViewCell*)tableView:(UITableView*)table cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary* chatMessage = [_conversation objectAtIndex:indexPath.row];
// Show If type is String
if ([chatMessage[#"type"] isEqualToString:#"string"]) {
// if its me
if([chatMessage[#"user_sender"] isEqualToString:_user_sender]){
NSString *CellIdentifier = #"fromMe";
FromMeTableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
[cell.message_me setNeedsLayout];
[cell.message_me layoutIfNeeded];
cell.message_me.clipsToBounds = YES;
cell.message_me.textContainerInset = UIEdgeInsetsMake(8.0, 8.0, 7.0, 8.0);
cell.message_me.text = chatMessage[#"msg"];
cell.message_date_me.text = [NSString stringWithFormat:#"%#", chatMessage[#"message_date"]];
cell.avatar_message_me.clipsToBounds = YES;
cell.avatar_message_me.layer.cornerRadius = cell.avatar_message_me.frame.size.width /2;
cell.avatar_message_me.image = [UIImage imageNamed:#"amber.png"];
return cell;
}
// it its other user
else {
NSString *CellIdentifier = #"fromThem";
FromThemTableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
[cell.message_them setNeedsLayout];
[cell.message_them layoutIfNeeded];
cell.message_them.clipsToBounds = YES;
cell.message_them.textContainerInset = UIEdgeInsetsMake(8.0, 8.0, 7.0, 8.0);
cell.message_them.text = chatMessage[#"msg"];
cell.message_date_them.text = [NSString stringWithFormat:#"%#", chatMessage[#"message_date"]];
cell.avatar_message_them.clipsToBounds = YES;
cell.avatar_message_them.layer.cornerRadius = cell.avatar_message_them.frame.size.width /2;
cell.avatar_message_them.image = [UIImage imageNamed:#"jenny.png"];
return cell;
}
}
// Show if type is Image File
else if ([chatMessage[#"type"] isEqualToString:#"img"]){
// if its me
if(![chatMessage[#"user_sender"] isEqualToString:_user_sender]){
NSString *CellIdentifier = #"fromThemImage";
FromThemImageTableCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
cell.image_type_them.image = [UIImage imageNamed:#"foto.jpeg"];
cell.message_date_them.text = [NSString stringWithFormat:#"%#", chatMessage[#"message_date"]];
return cell;
}
// if its other user
else {
NSString *CellIdentifier = #"fromMeImage";
FromMeImageTableCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
cell.image_type_me.image = [UIImage imageNamed:#"foto.jpeg"];
cell.message_date_me.text = [NSString stringWithFormat:#"%#", chatMessage[#"message_date"]];
return cell;
}
}
else if ([chatMessage[#"type"] isEqualToString:#"file"]){
NSLog(#"Type: %#", chatMessage[#"type"]);
NSString *CellIdentifier = #"fromThemFile";
FromThemFileTableCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
cell.message_date_them.text = [NSString stringWithFormat:#"%#", chatMessage[#"message_date"]];
cell.file_name_them.text = chatMessage[#"msg"];
cell.file_type_them.contentMode = UIViewContentModeScaleAspectFill;
NSArray *formats = [chatMessage[#"msg"] componentsSeparatedByString:#"."];
// PDF Format
if ([formats[1] isEqualToString:#"pdf"]) {
cell.file_type_them.image = [UIImage imageNamed:#"pdf.png"];
}
// Word Format
if ([formats[1] isEqualToString:#"doc"]) {
cell.file_type_them.image = [UIImage imageNamed:#"doc.png"];
}
if ([formats[1] isEqualToString:#"docx"]) {
cell.file_type_them.image = [UIImage imageNamed:#"doc.png"];
}
// Power Point Format
if ([formats[1] isEqualToString:#"pptx"]) {
cell.file_type_them.image = [UIImage imageNamed:#"ppt.png"];
}
if ([formats[1] isEqualToString:#"ppt"]) {
cell.file_type_them.image = [UIImage imageNamed:#"ppt.png"];
}
// Excel Format
if ([formats[1] isEqualToString:#"xls"]) {
cell.file_type_them.image = [UIImage imageNamed:#"xls.png"];
}
if ([formats[1] isEqualToString:#"xlsx"]) {
cell.file_type_them.image = [UIImage imageNamed:#"xls.png"];
}
return cell;
}
else {
// Remember to set this as a default value
NSString *CellIdentifier = #"fromThem";
FromThemTableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
cell.message_them.text = chatMessage[#"msg"];
cell.message_date_them.text = [NSString stringWithFormat:#"%#", chatMessage[#"message_date"]];
return cell;
}
}
The constraints I've been using for the textview to be able to expand and look like this is the following.
http://i.imgur.com/Kjva3ES.png
Any Idea how to fix this on a decent way..?
If your UITableViewCells vary in height due to variations in content you need to implement
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath;
to tell UITableView the height for each row.
In one of my apps this mainly involved calling
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context;
to predict the area needed by my expanding text field, and then adding in some margin.
I am Updating a iOS5 project to storybaord and compatible to run with iOS6 / iOS7. my table view have 4 sections and each section have data of different length which may or may not be displayed as a single line.
the old code is some thing like this and I get a warning for the statement
CGSize size = [productToShow.pDesc sizeWithFont:self.lblHidden.font constrainedToSize:self.lblHidden.frame.size lineBreakMode:NSLineBreakByWordWrapping];
warning
sizeWithFont:constrainedToSize:lineBreakMode:is first deprecated in iOS7
so I have tried usingboundingRectWithSize:options:attributes:context: as suggested by the compiler.
but the code seems to be more complicated: apple wouldn't be making simpler things difficult. if some one can see my codes and suggest the best method or a better and simple method it would be of grate help to me and the community.
iOS5 code with XIBs
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
NSString *tempPointStr = [self.shortDescArray objectAtIndex:indexPath.row];
self.lblHidden.frame = CGRectMake(58, 0, 945, 9999);
self.lblHidden.text = tempPointStr;
CGSize size = [tempPointStr sizeWithFont:self.lblHidden.font
constrainedToSize:self.lblHidden.frame.size
lineBreakMode:NSLineBreakByWordWrapping];
if (size.height < 50.0f)
{
return 50.0f;
}
else
{
return size.height;
}
}
elseif(indexPath.section == 1)
{
...
}
else
{
...
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
static NSString *tempPoint = #"ProductPointsCell";
ProductPointsCell *cell = (ProductPointsCell*)[tableView dequeueReusableCellWithIdentifier:tempPoint];
if (cell == nil)
{
NSArray* nib = [[NSBundle mainBundle] loadNibNamed:#"ProductPointsCell_iPad" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.showsReorderControl = NO;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor=[UIColor clearColor];
cell.lblPoint.font = [UIFont fontWithName:#"Arial-BoldMT" size:18.0f];
}
NSString *tempPointStr = [self.shortDescArray objectAtIndex:indexPath.row];
cell.lblPoint.text = tempPointStr;
self.lblHidden.frame = CGRectMake(58, 0, 945, 9999);
self.lblHidden.text = tempPointStr;
CGSize size = [tempPointStr sizeWithFont:self.lblHidden.font
constrainedToSize:self.lblHidden.frame.size
lineBreakMode:NSLineBreakByWordWrapping];
if (size.height < 50.0f)
{
cell.lblPoint.frame = CGRectMake(58, 0, 945, 50.0f);
}
else
{
cell.lblPoint.frame = CGRectMake(58, 0, 945, size.height);
}
return cell;
}
elseif(indexPath.section == 1)
{
...
}
else
{
...
}
}
where as the same code in :
iOS6/7 codes with storybaord
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
NSString *tempPointStr = (self.shortDescArray)[indexPath.row];
self.lblHidden.frame = CGRectMake(58, 0, 945, 9999);
self.lblHidden.text = tempPointStr;
CGSize constraint = CGSizeMake(945, 9999.0f);
NSDictionary * attributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14.0f] forKey:NSFontAttributeName];
NSAttributedString *attributedText = [[NSAttributedString alloc]initWithString:self.lblHidden.text attributes:attributes];
CGRect rect = [attributedText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGSize size = rect.size;
if (size.height < 50.0f)
{
return 50.0f;
}
else
{
return size.height;
}
}
elseif(indexPath.section == 1)
{
...
}
else
{
...
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
static NSString *tempPoint = #"ProductPointsCell";
ProductPointsCell *cell = (ProductPointsCell*)[tableView dequeueReusableCellWithIdentifier:tempPoint];
cell.showsReorderControl = NO;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor=[UIColor clearColor];
cell.pointLbl.font = [UIFont fontWithName:#"Arial-BoldMT" size:18.0f];
[cell.pointLbl setLineBreakMode:NSLineBreakByWordWrapping];
[cell.pointLbl setNumberOfLines:0];
NSString *tempPointStr = (self.shortDescArray)[indexPath.row];
cell.pointLbl.text = tempPointStr;
CGSize constraint = CGSizeMake(945,9999.0f);
NSAttributedString *attributedText = [[NSAttributedString alloc]initWithString:cell.pointLbl.text attributes:#{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]}];
CGRect rect = [attributedText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGSize size = rect.size;
if (size.height < 50.0f)
{
cell.pointLbl.frame = CGRectMake(58, 0, 945, 50.0f);
}
else
{
cell.pointLbl.frame = CGRectMake(58, 0, 945, size.height);
}
return cell;
}
elseif(indexPath.section == 1)
{
...
}
else
{
...
}
}
The purpose of these codes are to make the data displayed in a single line when it fits within a single line and when it doesn't the cell height is adjusted and the data is displayed as a multiline cell.
Also correct me if I should keep the autolayout on or off and should I set any parameters in the storyboard while converting.
if some one can suggest a better/faster and easier way please do so with examples.
Try it without NSAttributedString...this works for me:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSManagedObject *obj = [results objectAtIndex:indexPath.row];
NSString *text = [obj valueForKey:#"name"];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:FONT_SIZE], NSFontAttributeName, nil];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
CGFloat height = MAX(size.height, 45.0f);
return height ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TVCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TVCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSManagedObject *obj = [results objectAtIndex:indexPath.row];
NSString *text = [obj valueForKey:#"name"];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:FONT_SIZE], NSFontAttributeName, nil];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
[cell.cellText setText:text];
[cell.cellText setFrame:CGRectMake(CELL_CONTENT_MARGIN, 0.0, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 45.0f))];
return cell;
}
In TVCell.m:
self.cellText = [[UILabel alloc] initWithFrame:CGRectZero];
[self.cellText setLineBreakMode:NSLineBreakByWordWrapping];
[self.cellText setMinimumScaleFactor:FONT_SIZE];
[self.cellText setNumberOfLines:0];
[self.cellText setFont:[UIFont systemFontOfSize:FONT_SIZE]];
self.cellText.textColor = [UIColor colorWithRed:64/255.0 green:59/255.0 blue:59/255.0 alpha:1.0];
[self.contentView addSubview:self.cellText];
Guys can someone 'tell me why I can not align well the label in these cell? ... The space between the black and the yellow label should be the same but I can not figure out the exact point you want to change in the code ... Surely it is a trivial but I'm lost
This is the code I'm using
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *comment = [[self.objects objectAtIndex:[indexPath row]] objectForKey:#"TITOLO"];
CGFloat whidt = 220;
UIFont *FONT = [UIFont systemFontOfSize:13];
NSAttributedString *attributedText =[[NSAttributedString alloc] initWithString:comment attributes:# { NSFontAttributeName: FONT }];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){whidt, MAXFLOAT}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
return size.height +50;
}
- (FFCustomListaEsamiCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
FFCustomListaEsamiCell *cell = (FFCustomListaEsamiCell * )[self.tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (!cell) {
cell = [[FFCustomListaEsamiCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
NSString *text = [object objectForKey:#"TITOLO"];
CGSize constraint = CGSizeMake(220 , 20000.0f);
UIFont *FONT = [UIFont systemFontOfSize:13];
CGSize size = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:FONT }context:nil].size;
cell.FFTitoloEsameLabel.frame = CGRectMake(17,10, 240, MAX(size.height, 10.0)+10.0 );
cell.FFTitoloEsameLabel.text = text;
NSDateFormatter *FFDataFormattata = [[NSDateFormatter alloc] init];
[FFDataFormattata setDateFormat:FF_DATE_FORMATTER];
cell.FFDataEsameLabel.text = [NSString stringWithFormat: #"%#",[FFDataFormattata stringFromDate:[object objectForKey:FF_ESAMI_DATA_STRING]]];
PFObject *rowObject = [self.objects objectAtIndex:indexPath.row];
if([[rowObject objectForKey:FF_ESAMI_STATUS] boolValue]) {
cell.last.image = [UIImage imageNamed:#"FFIMG_ClockTAG"];
}
else
{
}
return cell;
}
I solved it by removing the auto-layout and setting the blocks with the size inspector ... I think it's the best solution and that is not a problem especially ... I hope ...