I am having table view.
I want to dynamically increase the height of the row to some size.
How can I do this.
I know its a silly question.But still can't find the solution.Help me please.
I have used this code.But it is not working :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *lblOutTime = [[UILabel alloc]init];
lblOutTime = [[UILabel alloc] initWithFrame:CGRectMake(2, 20, 200, 40)];
lblOutTime.text =#"Outtime : ";
lblOutTime.backgroundColor = [UIColor clearColor];
lblOutTime.font = [UIFont boldSystemFontOfSize:5.0f];
lblOutTime.font = [UIFont fontWithName:#"HelveticaNeue Heavy" size:5.0f];
[cell.contentView insertSubview:lblOutTime atIndex:1];
return cell;
}
-
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == myIndexPath)
{
return someHeight;
}
return normalHeight;
}
In this case the height is not dynamic. here
is only 2 fixed positions of cell height.
as the if conditions shows.
Here you can change the size of the cell:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.myRow == myCellIndex)
return NEW_HEIGHT;
else return NORMAL_HEIGHT;
}
And you can reload your table whenever you want with:
[myTable reloadData];
add this tableview delegate method to your class.m file.
Make sure your class conforms to UITtableViewDelegate protocol
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == myIndexPath)
{
return someHeight;
}
return normalHeight;
}
If You Want Increase the height of all row use
[tableView setRowHeight:44];
[tableView reloadData];
if You want increase the height of a particular row use UITableView Delegete heightForRowAtIndexPath:
and use this
[tableView reloadData];
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row <[ListOfCartItems count])
{
CGFloat result = 44.0f;
NSString* text = nil;
CGFloat width =220 ;
clsCart *tempCartPro;
tempCartPro=[ListOfCartItems objectAtIndex:indexPath.row];
text=tempCartPro.clsProductItem.productName;
if (text)
{
CGSize textSize = { width, 20000.0f }; // width and height of text area
CGSize size = [text sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 90.0f; // top and bottom margin
result = MAX(size.height, result) ; // at least one row
}
return result;
}
else
{
return 230.0;
}
}
Related
I have a problem with a UITableView with custom UITableViewCell.
The table is filled by an NSArray, and I want that if an object in this NSArray begins with - changes its appearance.
the problem is that the UITableViewCell that begins with - is changed, but also change other cells that should not change.
this is my code:
//this is the way in which change the height of the cell if the object in the array begins with -
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *try = [arrayTitleEs objectAtIndex:indexPath.row];
if ([[try substringToIndex:1]isEqualToString:#"-"]) {
return 45;
}
else return 160;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellCardioScheda";
CardioSchedaCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.titleEs.text = [arrayTitoloEs objectAtIndex:indexPath.row];
NSString *try = [arrayTitoloEs objectAtIndex:indexPath.row];
if ([[try substringToIndex:1]isEqualToString:#"-"]) {
cell.titleEs.frame = CGRectMake(0, 0, cell.frame.size.width-15, cell.frame.size.height);
}
return cell;
}
as you can see from the picture that begins with the cell - is smallest and the text is moved to the left, in the next cell is all right, but the text in the last cell is moved, but should not!
thanks to all
In cellForRowAtIndexPath you can create two different types of cells and return one or the other according to your needs:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *firstCell = [tableView dequeueReusableCellWithIdentifier:#"firstCellID"];
if (firstCell == nil) {
firstCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"firstCellID"] autorelease];
}
// Set here firstCell
UITableViewCell *secondCell = [tableView dequeueReusableCellWithIdentifier:#"secondCellID"];
if (secondCell == nil) {
secondCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"secondCellID"] autorelease];
}
// Set here secondCell
if ([[try substringToIndex:1]isEqualToString:#"-"]) {
return secondCell;
} else {
return firstCell;
}
}
The problem is with the following:
if ([[try substringToIndex:1]isEqualToString:#"-"]) {
cell.titleEs.frame = CGRectMake(0, 0, cell.frame.size.width-15, cell.frame.size.height);
}
You need an else to set the frame properly if the condition isn't met. Cells get reused. Anything you do to a cell must be done for all cells.
if ([[try substringToIndex:1]isEqualToString:#"-"]) {
cell.titleEs.frame = CGRectMake(0, 0, cell.frame.size.width-15, cell.frame.size.height);
} else {
cell.titleEs.frame = CGRectMake(...); // whatever regular cells should be
}
BTW - you could replace [[try substringToIndex:1]isEqualToString:#"-"] with [try hasPrefix:#"-"].
// I am loading **HTML file** in **UIWebview** which is located on **tableviewcell**.
I want to fix height for cell based on HTML file height.
Note: loading HTML file will be different for every cell.(height is not constant for every HTML file)
To get the height of UIWebView object, first you need to load them. Then inside the delegate method of UIWebView you can get the height as per the html content like given below.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"%f",myWebView.scrollView.contentSize.height);
}
you can also get the height of UIWebView by JS insertion like this
[myWebView stringByEvaluatingJavaScriptFromString:#"document.body.offsetHeight;"];
In webViewDidFinishLoad method you have to store height based on webview object tag.
After that load your table and in below method give height accordingly.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
If I understood correctly..
Here you sets cell.textLabel.lineBreakMode and number of lines, for cell.textLabel. (0 - infinity)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
}
cell.textLabel.text = [news objectAtIndex:indexPath.row];
return cell;
}
Here you need to count the height of cell.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = [news objectAtIndex:indexPath.row];
UIFont *cellFont = [UIFont fontWithName:#"Helvetica-Bold" size:20.0f];
CGSize constraintSize = CGSizeMake(320.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 20.0f;
}
Implement the UITableViewDelegate method - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [self getItemForKey:kSummary];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
//You will need to define kDefaultCellFont
CGSize labelSize = [text sizeWithFont:kDefaultCellFont
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + ANY_OTHER_HEIGHT;
}
If you want more check the Link
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to adjust the hieght of the cell table when loading from the custom cells?
I have an application in which I am loading the cell from different custom class object.It was working fine.But my problem is I am adding a text view on this subclass. I need to adjust the height of the cell in the table view according to the contents of that text view.`
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
return 125;
}
else
{
return 245;
}
}
Now I am doing like this. But I want the cell height to be changed according to the text view content size in the custom class cell. Can anybody help me?
You can do it in easy way just look at this :
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tv dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"Cell"] autorelease];
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
[[cell contentView] addSubview:label];
}
}
then u can Calculate the Cell Height and here an full example link
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
i ve got an imageview ..below the imageview is a table view..which loads the data from server.by default five fields in table view are visible ..works fine..if i m to scroll the table view to view the remaining fields..the scroller rebounces..wat i want is the table view to be fixed and not to rebounce when scrolled ..i d be so greatful if u guys cud help me out..below is the code..
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [items count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 28.0;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,
tableViewteam.bounds.size.width, 28)] autorelease];
headerView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"team"ofType:#"png"]]];
return headerView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.imageView.backgroundColor=[UIColor clearColor];
cell.textLabel.text=[[items objectAtIndex:indexPath.row]objectForKey:#"title"];
cell.textLabel.textColor=[UIColor whiteColor];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
I didn't fully understand your problem. If you want to stop bouncing the table view you can add
yourTableView.bounces = NO;
in ViewDidLoad()
If you need your tableView to be fixed you can disable scrolling.
tableView.scrollEnabled = NO;
i have table view in my app and each row have long text so its possible to scroll whole table so user can read entire text
also i am using following code to set up my cell
-
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//buttonCount=0;
static NSString *CellIdentifier = #"Cell";
static NSUInteger const kLeftLabel = 100;
static NSUInteger const kRightLabel = 101;
row = [indexPath row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
CGRect leftFrame = CGRectMake(2, 5, 45, 30);
CGRect rightFrame = CGRectMake(50, 5, 400, 30);
left = [[[UILabel alloc]initWithFrame:leftFrame]autorelease];
left.tag = kLeftLabel;
left.font = [UIFont systemFontOfSize:13];
[cell.contentView addSubview:left];
right = [[[UILabel alloc]initWithFrame:rightFrame]autorelease];
right.tag=kRightLabel;
right.font = [UIFont systemFontOfSize:13];
[cell.contentView addSubview:right];
}
else {
left = (UILabel*)[cell.contentView viewWithTag:kLeftLabel];
right=(UILabel*)[cell.contentView viewWithTag:kRightLabel];
}
left.text =[secondSplitArrayValue objectAtIndex:row];
right.text=[splitArrayValue objectAtIndex:row];
if (indexPath.row == 0)
{
[cell setSelectionStyle:UITableViewCellSelectionStyleNone ];
}
return cell;
}
by use of above code i have two column so 1st column have small text but 2nd is very much big and to read i want to scroll it horizotally.
i hope some one will solve this problem.
thank you in advance
To solve your problem there is no need to modify scrolling of table view you can do it by following code... by using this code you can show ur text in multiline in the default label of table view cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"MyCell";
UITableViewCell *cell =(UITableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = #"Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. Its for testing. ";
return cell;
}
You need to use the multiline label for the long text and set the height of your long cell properly by calculating with long text in below function.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
for more you can read the tutorial
place your UITableView in UIScrollview
But looking at your case there is no need to do such thing, I would prefer to make the cells height Dynamic according to the Text.
Just use above code which might help to solve your problem:
You need to use Custom Cell having TextView in cell;
//Calculate the Height & Width of Text
CGSize stringSize = [#"Your sample text whose height & Width need to be calulated" sizeWithFont:[UIFont boldSystemFontOfSize:[YurTextField font] constrainedToSize:CGSizeMake(230, 9999) lineBreakMode:UILineBreakModeWordWrap];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"MyCell";
UITableViewCell *cell =(UITableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[Over here take TextView calculating the Height & width of String ];
// [add TextView it cells contentView];
[cell.contentView addSubview:TextView];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
CGSize stringSize = [#"Your sample text whose height & Width need to be calulated" sizeWithFont:[UIFont boldSystemFontOfSize:[YurTextField font] constrainedToSize:CGSizeMake(230, 9999) lineBreakMode:UILineBreakModeWordWrap];
return stringSize.height;
}
if it doesn't solve your purpose then try using below alternative
Although I can suggest you to use the ScrollView & set it Delegate.
Add your UITAbleView as SubView to ScrollView & keep the size of scrollview according to the view how much you want to Scroll.
->View
-->ScrollView
--->UITableView