Hindi Text Lagging in tableview - ios

I have List of Hindi Text from Web-serviceAnd want to display in UILabel.
I am trying this using following code.
For testing I set static text Like:
NSString *unicode=#"ये भी एक तमाशा है इश्क और मोहब्बत में... - ये भी एक तमाशा है इश्क और मोहब्बत में दिल किसी का होता है और बस किसी का चलता है.";
cell.lblPostContent.text = unicode;
In this way problem is that...My UITableview is not smoothly scrolling. too much lag in UITableView. Is there any way to display this text properly in Hindi language and also to have smooth scrolling on UITableView?
This is My cellForRowAtIndexPath........................
- (UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell=(GroupDetailCustomeCell*)[tableview dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil){
cell=[[GroupDetailCustomeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
NSString *unicode=[[post.postContent URLDecode] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if(unicode==nil)
unicode=[post.postContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if([post.postType isEqualToString:#"0"]){
//textonly
cell.lblPostContent.text =unicode;
}
}
return cell;
}

tableView component is a most hard to learn component in whole platform as 90% of iOS applications are data in a tableView. There are many things which could provide this incorrect behaviour. You could possibly try some of that options to fix it:
Most common reason for this type of problems in tableView is heightForRowAtIndexPath method. Try to override it and count real cells height there.
Example:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *yourHindiText = [NSString stringWithString:#"some text"];
CGSize yourTextSize = [yourHindiText sizeWithFont:[UIFont fontWithName: #"SomeFont" size:someSize] constrainedToSize:kLabelFrameMaxSize];
return self.tableView.estimatedRowHeight + yourTextSize.height;
}
Try to use estimatedRowHeight property for your cells. As the Apple said, this property providing a nonnegative estimate of the height of rows can improve the performance of loading the table view. If the table contains variable height rows, it might be expensive to calculate all their heights when the table loads. Using estimation allows you to defer some of the cost of geometry calculation from load time to scrolling time.
When you create a self-sizing table view cell, you need to set this property and use constraints to define the cell’s size.
Example:
self.tableView.estimatedRowHeight = 44.0; //in your viewDidLoad method
Always use dequeueReusableCellWithIdentifier:forIndexPath: method in your cellForRowAtIndexPath: if you still not.
Example:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; //in your cellForRowAtIndexPath method.
You may set the row height for cells if the delegate doesn’t implement the tableView:heightForRowAtIndexPath: method with rowHeight property.
Example:
self.tableView.rowHeight = UITableViewAutomaticDimension; //in your viewDidLoad method
There are could be more and more solutions for this problem (like using Core Text Framework or creating layers for using GPU instead of CPU to offload it etc).
You could find plenty of possible solutions here. Please pay attention on the first one.

If limiting is not a problem, you can limit Hindi characters to improve UITableView scroll lag.
Here is the code to limit in swift 3.
extension String
{
func limit(by:Int) -> String {
let startIndex = self.startIndex
let endIndex = self.index(startIndex, offsetBy: by)
let range = Range(uncheckedBounds: (lower:startIndex , upper: endIndex))
return self[range]
}
}
Hope this helps.

Related

How to get cell in heightForRowAtIndexPath?

I have created custom cells in my app.I want to get the each cell in HeightForRowAtIndexPath.Please tell me how can i get the custom cell in this method.I have tried this code but this causes infinite loop & finally crash the app.
HomeCell *cell=(HomeCell *)[tableView cellForRowAtIndexPath:indexPath];
EDIT:
I Have tried this but it gives me cell height as zero.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"HomeCell";
HomeCell *cell = (HomeCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
float tv_view_height=cell.tv_post.frame.size.height;
float like_count_height=cell.label_like_count.frame.size.height;
float first_comment_height=cell.first_comment.frame.size.height;
float second_comment_height=cell.second_cmment.frame.size.height;
float third_comment_height=cell.third_comment.frame.size.height;
Post *user_post=[arr_post objectAtIndex:indexPath.row];
float comment_count=[user_post.comment_count intValue];
if(comment_count<=0)
{
first_comment_height=0;
second_comment_height=0;
third_comment_height=0;
}
else if(comment_count==1)
{
second_comment_height=0;
third_comment_height=0;
}
else if(comment_count==2)
{
third_comment_height=0;
}
float like_count=[user_post.like_count intValue];
if(like_count<=0)
{
like_count_height=0;
}
float total_height=tv_view_height+like_count_height+first_comment_height+second_comment_height+third_comment_height;
NSLog(#"total heigh is %f'",total_height);
return total_height;
}
Please tell which is the best way?
How to get cell in heightForRowAtIndexPath?
It's impossible, because when -heightForRowAtIndexPath is called, no cells are created yet. You need to understand how the UITableView works:
UITableView asks it's datasource how many sections it will have
-numberOfSectionsInTableView
At this point there are no cells created.
UITableView asks it's datasource how many rows each section will have
-numberOfRowsInSection
At this point there are no cells created.
UITableView asks it's delegate height of each visible row, to know where cells will be located
-heightForRowAtIndexPath
At this point there are no cells created.
UITableView asks it's datasource to give it a cell to display at given index path
-cellForRowAtIndexPath
At this point the cell is created.
The height of each cell you can calculate from data model. You don't need the cell – you already know the frame width that will contain a comment, you know it's content, you know it's font, you know linebreak mode, etc. So, you can calculate height. For example:
CGFloat commentsHeight = 0;
Post *user_post = [arr_post objectAtIndex:indexPath.row];
for (NSString *comment in user_post.comments)
{
CGRect commentrect = [comment boundingRectWithSize:CGSizeMake(self.view.bounds.size.width - 18, FLT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin)
attributes:#{NSFontAttributeName:[UIFont systemFontOfSize:15]}
context:nil];
commentsHeight += commentrect.size.height;
}
And you can calculate height of the other components of cell from its data model.
But now, in 2015, it's not the best way. You really would be better to read the tutorials, which showed #Zil, and do it with Autolayout.
You should declare an array for storing TableView cells in cellForRowAtIndexPath and you can use stored cells in heightForRowAtIndexPath. Lets Try using this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"HomeCellID";
HomeCell *cell = (HomeCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[[HomeCell alloc] init] autorelease];
}
// Store table view cells in an array
if (![tableViewCells containsObject:cell]) {
[tableViewCells addObject:cell];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([tableViewCellsArray objectAtIndex:indexPath.row]) {
HomeCell *cell = (HomeCell *)[tableViewCells objectAtIndex:indexPath.row];
// Process your Code
}
return yourCalculatedCellHeight;
}
I would recommend you to take the height form a configuration collection on your viewController.
Something like this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height;
CellConfiguration * selectedCellConfiguration =[_cellConfigurations objectAtIndex:indexPath.row];
switch (selectedCellConfiguration.type) {
case TheTypeYouNeed:
return TheValueYouNeed
default:
height = 44.0f;
break;
}
return height;
}
You could create a new cell from scratch, simply by HomeCell *sexyCell = [[HomeCell alloc]init];
or dequeue one like you did in cellForRow (tableView.dequeueWithReuseIdentifier:).
Though I advise creating one from scratch and disposing it after (setting it to nil), because if you dequeue it there they'll go in queue and cause heavy memory leaks and end up with many cells for the same indexPath.
What you COULD do is the following :
Create a cell with alloc init
Fill it with the real data
use .layoutsubviews on its view
calculate it's size and apply it to your real cell
What you SHOULD do :
Use auto layout and add all the constraints that are necessary, all your labels will size dynamically. It takes about 3 or 4 hours to get the basics of Auto layout, and about a month of regular use to really get the hang of it with ease.
I strongly strongly strongly suggest you do NOT resize using the frame of objects, most labels and views will resize like they should without having to write any code if you use constraints properly.
Once you have done that, because you have cells of varying heights, is using the DynamicHeight property of the tableview and the slight adjustements that comes with it. You can find
A great tutorial here
The same updated tutorial for swift (more up to date but you'd need to translate)
This amazing StackOverflow answer which you MUST read
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:indexpath.row inSection:0];
Custom Cell *cell = [tableview cellForRowAtIndexPath:indexPath];
By this , you will get each cell in your method

Static UITableView with custom row heights

I'm trying to create detail view controller as a list of information and I think it would be nice and clean to present this with a static UITableView. But after that it came to my mind that on some level it might be difficult, so please resolve my doubts!
Every UITableViewCell has different style (some are custom, some are basic and few are right-detailed etc.).
What is more, content size of each cell may vary as I have long names put inside labels so they use autolayout to fit.
There is no problem when I have the same cells repeating but with different tex inside UILabels. In that case I use a simple:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.prototypeCell) {
self.prototypeCell = [self.tableView dequeueReusableCellWithIdentifier:#"ActivityCell"];
}
[self fetchedResultsController:[self fetchedResultsController] configureCell:self.prototypeCell atIndexPath:indexPath];
CGSize size = [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height;
}
I don't know how to deal with heightForRowAtIndexPath. I can give an identifier to each cell, call cellForRowAtIndexPath:, and make a big switch or if statement, but is it right? The same problem occurs while I think of cellForRowAtIndexPath: and populating those UITableViewCells. With those testing statements this code won't be pretty and readable.
Any ideas on that case?
In the delegate function of the table view named heightForRowAtIndexPath try to calculate the height for each row and then return it.
//return height for row
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView==tblLanguage)
{
//Here calculate the dynamic height according to songs count for specific language
return (([[arrSongListForSpecificLanguage objectAtIndex:indexPath.row] count]*40)+40);
}
return 40.0;
}

Dynamic sized labels inside a table not refreshing in ios

My table has cells which have several labels. I want one of these labels to fit its size so text begins right below the Title (remember that Labels align text vertically unless you fit its container).
Problem is, the very first time the table is loaded all labels' texts are succesfully populated but label sizes don't actually graphically apply until the NEXT time a refresh is asked. (if I ask for a reloadData with the exact same information, the labels' sizes work flawlessly).
This is some of my cellForRowAtIndexPath code:
cell.body.text = user.message;
[cell.body sizeToFit];
The only solution I've found so far is double calling [table reloadData] but this is an ugly solution. Any way I can fix this?
Edit: Previous code was a summary, I'll show the whole code here as requested:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TwitterTweetCell *cell = [tableView dequeueReusableCellWithIdentifier:#"TwitterTweetCell"];
// Populate cell
TweetModelData *tweet = [self.twitterModelData.tweets objectAtIndex:[indexPath item]];
cell.tweetName.text = tweet.user;
cell.tweetChannel.text = tweet.userName;
cell.tweetBody.text = tweet.message;
[cell.tweetBody sizeToFit];
return cell;
}
Regarding cell size, everything is working ok. Depending on the size of the message each cell has a different size which was pre-calculated before.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ((TweetModelData *)[self.twitterModelData.tweets objectAtIndex:[indexPath item]]).tweetHeight + 30.0f;
}
I finally found it. Don't know the reason, but it seems disabling "Use autolayout" on my storyboard fixed it.
I'm guessing auto-layout was overwriting the layout changes I was applying so they had no effect until the next data reload.

Tableview showing the wrong size of cell [duplicate]

This question already has answers here:
UITableViewCell frame height not matching tableView:heightForRowAtIndexPath:
(3 answers)
Closed 9 years ago.
in table i am setting the height for cell using(heightForRowAtIndexPath)delegate of table view
the code is:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
but i am checking the size of cell in delegate method(cellForRowAtIndexPath) and code is:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Dequeue or create a cell
UITableViewCellStyle style = UITableViewCellStyleDefault;
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:#"BaseCell"];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:#"BaseCell"] ;
cell.textLabel.text = [NSString stringWithFormat:#"Cell %d", numberOfItems - indexPath.row];
NSLog(#"%f",cell.frame.size.height);
return cell;
}
when i am printing the value(frame of cell)cell on console its giving me 44.00.why this is happening even i am setting height of cell..please explain me and what to do to get the cell oh height 100..thanks in advance
actually i want to make custom type table view which support difrrent orientation of view and it is universal app so it will better to call the cell size in behalf of checking every time (iphone/ipad,diff orintation)....plz help me to accomplish requirement
If the cell is being shown correctly, and by correctly I mean with a height of 100 pixels as you have written in your tableView:heightForRowAtIndexPath:, I'm pretty sure it's because you're asking for the cell's height in the wrong place:
the cell has just been init'd with the default init method, the height returned is therefore the default one, of 44 pixels as nslog prompts in your console, on rendering the delegate sets the right height returned from your method and everything is set up correctly.
I had this issue months ago, for some reasons I needed to know cell's height in the tableView:cellForRowAtIndexPath: method, I came out with a workaround for that: I've stored all rowHeights values in an NSArray, since they were dynamic and different row by row according to their content.
I came out with something like
CGFloat height = [[heightsData objectAtIndex: indexPath.section] objectAtIndex: indexPath.row];
Do you have set your delegate for UITableViewDelegate ?
Try to put any log in your tableView:heightForRowAtIndexPath: method at first to see if your delegate is set.
hi friends dev had given explanation
NSLog(#"%f",[self tableView:tableView heightForRowAtIndexPath:indexPath]);

iPhone + UITableView

I am having UITableView in my application, I want to do formatting of it, like change the height of rows in table, change the font and colors of text in cells etc.
It sounds like you should read A Closer Look at Table-View Cells in Apple's Table View Programming Guide for iOS.
Changing the Text Colour and Font
If you're using a standard table view cell, you can customise the text colour and font of its textLabel (or detailLabel) label in the tableView:cellForRowAtIndexPath: data source method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
/* Cell initialisation code... */
/* Configure Cell */
[[cell textLabel] setTextColor:[UIColor grayColor]];
[[cell textLabel] setFont:[UIFont fontWithName:#"Marker Felt" size:22]];
return cell;
}
Changing the Height of a Row
If every row is going to be the same height, you should set the rowHeight property of your UITableView:
[tableView setRowHeight:42];
If the rows are going to have variable heights, then you can use the tableView:heightForRowAtIndexPath: delegate method of UITableView:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = 42;
if ([indexPath row] == 4) {
height = 21;
}
return height;
}
If you want to completely change the look of your table view cells, you might want to look at Matt Gallagher's Easy custom UITableView drawing article, as suggested by #mfu. However, make sure you know exactly what you're doing if you start going this far—most of the time you will want to stick to Apple's default styles.
Your question is rather general.
You can refer to very nice article here to get some basic idea on how you can write custom table view 'easy custom uitableview drawing'
You should look at subclassing UITableViewCell and with that new subclass you can have anything you want inside the cell - other views, buttons, labels, etc.
Apple has many good samples of this. See this for a list of samples.

Resources