Font size in UITextView change unexpectedly - objective-c

I have a table view with several table view cell. The table view cell consists of title (using UILabel), picture (using UIWebView) and summary (using UITextView).
When I use UIImage for the picture, everything runs well. But then, I decided to display the pic by using resized UIWebView instead of UIImage. Some of the summary in the UITextView change unexpectedly.
When I scrolled the table, the problem will sometimes disappear. But it can randomly appear again on different table view cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
TableViewCell *cell = (TableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"TableViewCell" owner:self options:nil];
cell = table;
self.table = nil;
}
//Configure the cell...
Article *article = [_articleRepository fetchArticleById:[articleIds objectAtIndex:indexPath.row]];
cell.headline.text = article.articleTitle;
// Remove html tag
NSString *substringContent = [self stringByStrippingHTML:article.articleContent];
cell.content.text = substringContent;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
ImageRepository *imageRepository = [[ImageRepository alloc]init];
Image *image = [imageRepository fetchImageById:article.articleImage];
NSMutableString *mutUrl = [[NSMutableString alloc]initWithString:image.imageUrl];
[mutUrl insertString:#"_thumb" atIndex:[mutUrl length]-4];
NSString *stringUrl = [[NSString alloc]initWithString:mutUrl];
NSString *htmlImage = [NSString stringWithFormat:#"<html><head><style type=\"text/css\">body{margin: 0; padding: 0;}</style></head><body><img src=\"%#\"></body></html>", stringUrl];
[cell.image loadHTMLString:htmlImage baseURL:nil];
return cell;
}

It could be due to reuse of cells in the tableview that make ur problem appear randomly across cells. Would help if you posted code of what you've done.
EDIT: Try changing ur portion of the code to the one given below.
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"TableViewCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
Some questions:
1. Why are you doing cell = table & self.table = nil ?

Related

How Block or disable someones Customs Cells From Tableview with data from plist?

well I have 2 plist files:
And:
My StoryBoard is:
the problem is when in my "Inicio" viewcontroller load data from the plist, I want Disable the cells when have the ENABLE=NO property & Enable the cells with ENABLE = YES, when I push or click in the cell, then go to the next view controller "PERSONAS" in this view controller load the 2nd Plist and in the same case I want go to the "DETALLE"viewcontroller only with the enabled cells that have ENABLED YES from plist.
for each viewdidload Im use:
NSString *myListPath = [[NSBundle mainBundle] pathForResource:#"MainMenuList" ofType:#"plist"];
mainMenu = [[NSArray alloc]initWithContentsOfFile:myListPath];
NSLog(#"%#",mainMenu);
NSString *myListPath = [[NSBundle mainBundle] pathForResource:#"PersonasList" ofType:#"plist"];
Personas = [[NSArray alloc]initWithContentsOfFile:myListPath];
NSLog(#"%#",Personas);
And for show the tableview in the custom cell Im use:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"CustomCell";
NSLog(#"CARGANDO CELDAS");
MainMenuCell *cell = (MainMenuCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCelliPhone" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//CustomCell*cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.Enabled.text = [[mainMenu objectAtIndex:indexPath.row]objectForKey:#"ENABLE"];
cell.TitleLabel.text = [[mainMenu objectAtIndex:indexPath.row]objectForKey:#"NAME"];
cell.ImageImageView.image = [UIImage imageNamed:[[mainMenu objectAtIndex:indexPath.row]objectForKey:#"IMAGE"]];
if ([cell.Enabled.text isEqualToString:#"NO"])
{
NSLog(#"%#",cell.Enabled.text);
cell.userInteractionEnabled = NO;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.selected = NO;
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
in the Persona view controller I use the same code but not working, all customs cells are enabled, how can I fix that?, or am I wrong in something? or I did Forget Something please help guys!!
in the Simulator run in that way for ENABLE:
But I dont wanna the cells with ENABLE NO run!!:
Please help guys, im using XCODE 5 DP6, for iOS7, how is the best way for solve this!!!
THANKS!!!!
for many intents, finally fix this!! well in each viewcontroller the code whit solve this is like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"MainMenuCell";
NSLog(#"CARGANDO CELDAS");
MainMenuCell *cell = (MainMenuCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MainMenuCelliPhone" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//MainMenuCell*cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.Enabled.text = [[mainMenu objectAtIndex:indexPath.row]objectForKey:#"Enabled"];
cell.TitleLabel.text = [[mainMenu objectAtIndex:indexPath.row]objectForKey:#"Title"];
cell.ImageImageView.image = [UIImage imageNamed:[[mainMenu objectAtIndex:indexPath.row]objectForKey:#"Image"]];
//cell.ImageImageView.image = [[mainMenu objectAtIndex:indexPath.row]objectForKey:#"Image"];
if ([[[mainMenu objectAtIndex:indexPath.row]objectForKey:#"Enabled"] isEqualToString:#"NO"])
{
NSLog(#"%#",cell.Enabled.text);
cell.userInteractionEnabled = NO;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.selected = NO;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.ImageImageView.image = [UIImage imageNamed:#"NO.png"];
}
else
{
cell.ImageImageView.image = [UIImage imageNamed:#"YES.png"];
}
return cell;
//------------------------------------------//
// THANKS anyway //
// GRETTINGS FROM BOLIVIA //
// ROCK ON!!!! n_n' //
//------------------------------------------//
}

Asynchronous loading image inside uitableviewcell

Hello I am trying to use EGOImageView inside a CustomTableViewCell who i made to customize the cell. This is the code where I used the EGOImageView.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString* simpleTableIdentifier = #"Albums";
CustomTableCell* cell = (CustomTableCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (!cell)
{
cell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
NSLog(#"Show once or more times");
}
NSDictionary* dictionary = (NSDictionary*)[self.albumCollection objectAtIndex:indexPath.row];
cell.label.text = [dictionary valueForKey:#"name"];
EGOImageView* imageView = [[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageWithContentsOfFile:#""]];
[imageView setImageURL:[NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/picture?type=small&access_token=%#", (NSString*)[dictionary valueForKey:#"id"], [[FBSession activeSession] accessToken]]]];
[imageView setFrame:CGRectMake(0.0f,0.0f,78.0f,78.0f )];
[cell.iView addSubview:imageView];
[imageView release];
The image on each cell loading the same image. Would it be because it reused the cell while loading the image.
I found a problem I can't think of why the problem happened. I used the graph api to grab the image https://graph.facebook.com/%#/picture?type=small&access_token=%# where the first parameter was the album id.
To make myself easy to see the problem I only used one album on the cell, no matter what album i used the same photo turned up. But when I copy the link to the browser, the actual photo url shown on the address bar with the image shown and it shown the correct photos.
Does anyone know what was wrong.
Here is example. It loads user pics from some server in background and updates cell image. Note that imageView.image is set to nil at the beginning. This is dome for the case of cell reuse so that you will have no image rather than wrong image for the time while its downloading.
One more thing to add is that, would be also good to have a cache so that it does not download images all the time. Another nice thing is to not download images in edge networks.
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"TransactionCell";
NSMutableArray *data = searching ? searchResult : dataSource;
NSDictionary *object = [data objectAtIndex:[indexPath row]];
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithIdentifier:CellIdentifier] autorelease];
}
cell.imageView.image = nil;
cell.textLabel.text = #"Your cell text";
NSString *contact = #"foo#gmail.com";
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imgData = [appDelegate addUserPic:contact];
if (imgData == nil && netStatus == ReachableViaWiFi) {
NSString *url = [NSString stringWithFormat:#"http://somehost.com/userpic/%#", contact];
imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
}
dispatch_async(dispatch_get_main_queue(), ^{
UITableViewCell *updateCell = [self.tableView cellForRowAtIndexPath:indexPath];
if (updateCell) {
if (imgData) {
[appDelegate setUserPic:contact imgData:imgData];
updateCell.imageView.image = [UIImage imageWithData:imgData];
} else {
updateCell.imageView.image = nil;
}
/* This forces the cell to show image as now
it has normal bounds */
[updateCell setNeedsLayout];
}
});
});
return cell;
}

TableView with little less performance

I'll try to implement a TableView with 2 different kinds of cells, the first one is a higher Cell with a fullwidth image inside, all other cells got a little thumb image on the left side.
My problem it the performance of the TableView. If I scroll a list of maybe 20 items it jerks a bit. I've red something the performance and I'll hope the code is not so bad:
"Caching of images is not necessary to raise the performance" Is this right?
Does I reuse the cells in the right way.
Is that the normal way to use 2 different kinds of Cells?
Here is the important Part:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0 && indexPath.row == 0) {
return 160;
}
return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellName = [[NSString alloc] init];
// [...]
if(indexPath.section == 0 && indexPath.row == 0){
cellName = #"FirstMainCell";
CellTemplateFirstNews *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
if(cell == nil){
cell = [[[NSBundle mainBundle] loadNibNamed:#"CellTemplateFirstNewsView" owner:nil options:nil] objectAtIndex:0];
}
NSURL *urlRowImage = [NSURL URLWithString:[[NSString alloc]initWithFormat:#"http://webserver.de/inhalte/news/title/%#", detailDataNews.title_picture]];
NSData *dataRowImage = [NSData dataWithContentsOfURL:urlRowImage];
UIImageView *firstNewsImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 160)];
firstNewsImageView.backgroundColor = [UIColor clearColor];
firstNewsImageView.opaque = NO;
firstNewsImageView.image = [UIImage imageWithData:dataRowImage];
cell.backgroundView = firstNewsImageView;
// [...]
return cell;
}else{
cellName = #"MainCell";
CellTemplateNews *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
if(cell == nil){
cell = [[[NSBundle mainBundle] loadNibNamed:#"CellTemplateNewsView" owner:nil options:nil] objectAtIndex:0];
}
NSURL *urlRowImage = [NSURL URLWithString:[[NSString alloc]initWithFormat:#"http://webserver.de/inhalte/news/cover/%#", detailDataNews.cover_picture]];
NSData *dataRowImage = [NSData dataWithContentsOfURL:urlRowImage];
UIImage *rowImage = [UIImage imageWithData:dataRowImage];
cell.thumbImage.image = rowImage;
// [...]
return cell;
}
}r
I think the main problem is the synchronous network request that's happening for every cell, whether it's reused or not. The culprit is dataWithContentsOfURL:.
Check out this apple sample code, or google the phrase "ios lazy table images". In this SO answer, I provide a fairly simple to implement method that takes care of the loading an image asynch, then finding and updating the correct table view row upon completion.
One posibility is that you've forgotten to enter the CellIdentifier in your Xib. In fact the reuseIdentifier method needs your cell to have the same identifier, but you never set it when you create your cell. Those cells in your Xib have to have their identifier entered.
Do not alloc-init any object in cellForRowAtIndexPath You are probably leaking massive memory. Remember, this method called every time a cell comes into view, even cells that have been previously shown.
Also, your assignments to string variables are redundant. It is sufficient to do it like this.
BOOL firstCell = (indexPath.row==0 && indexPath.section==0);
NSString *cellIdentifier = firstCell ? #"FirstMainCell" : #"MainCell";
UITableViewCell *genericCell = [tableView
dequeueReusableCellWithIdentifier:cellIdentifier];
if (firstCell) {
cell = (CellTemplateFirstNews*) genericCell;
// configure...
}
else {
cell = (CellTemplateNews*) genericCell;
// configure...
}
return cell;
The other possible reason for your lag is the synchronous call to the web service. You are doing this each time the cell becomes visible. You need to load the images lazily and update the UI when necessary.

Is there a way to customize UISearchDisplayController cell

I am using UISearchDisplayController to search and would like to add so more elements to each cell if that is possible. Did some searching but couldn't find any information. The only way is to reproduce it with tableview. Is there any other properties i can set other then textlabel.text
Thanks in advance!
You can change the cell completely.
The following snippet relies on two subclasses of UITableCell, ANormalCell & ASearchCell.
Caveat: This code has not been compiled but you get the gist?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *normalCellReuseIdentifier = #"ANormalCell";
static NSString *searchCellReuseIdentifier = #"ASearchCell";
UITableViewCell* cell = nil;
if (tableView != self.searchDisplayController.searchResultsTableView) {
cell = (ANormalCell*)[self.tableView dequeueReusableCellWithIdentifier:normalCellReuseIdentifier];
if (cell == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ANormalCell" owner:nil options:nil];
cell = (ANormalCell*)[topLevelObjects objectAtIndex:0];
}
} else {
cell = (ASearchCell*)[self.tableView dequeueReusableCellWithIdentifier:searchCellReuseIdentifier];
if (cell == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ASearchCell" owner:nil options:nil];
cell = (ASearchCell*)[topLevelObjects objectAtIndex:0];
}
}
return cell;
}
Very possible. All of the tableView datasource methods are called with a tableView parameter. If
if (tableView == searchController.searchResultsTableView)
then alter the cell by adding subviews, or customizing properties. Same approaches apply for custom cells in this case as in any other.
Yes you can set cell.imageView , cell.accessoryView , cell.BackGroundView .. etc.
read this UITableViewCell documentation for further information

Double selection of a uitableviewcell in Custom Cell

i have an issue regarding UitableViewCell. the issue is that i have made a custom cell and i have a check box image button in that cell. i check and uncheck it. it works fine but the issue is that when i select Row # 1. it also selects row # 10 same goes with other rows like for 2 it will auto select row # 11. i am showing 10 rows at a time.
here is my code for CellForIndexPath
static NSString *cellIdentifier = #"Cell";
InterestsTableViewCell *cell = (InterestsTableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
NSArray *arrayNibs = [[NSBundle mainBundle] loadNibNamed:#"InterestsTableViewCell" owner:self options:nil];
cell = [arrayNibs objectAtIndex:0];
cell.delegate = self;
cell.total = [dataArray count];
}
cell.tag = indexPath.row;
cell.lblTitle.text = [dataArray objectAtIndex:indexPath.row];
if (indexPath.row==0) {
cell.imgBg.image = [UIImage imageNamed:#"tableCell_top_default.png"];
}else if(indexPath.row == [dataArray count]-1){
cell.imgBg.image = [UIImage imageNamed:#"tableCell_bottom_default.png"];
}else{
cell.imgBg.image = [UIImage imageNamed:#"tableCell_middle_default.png"];
}
return cell;
Plus i am detecting touch so that i can change the background image (i have an other image as background). Code for Touch Began (in custom cell is below)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//NSLog(#"%i",tag);
isSelected = !isSelected;
btnTickMark.selected = !btnTickMark.selected;
(isSelected ? (onImage = YES) : (onImage = NO));
Can somebody help me in this issue that why 2 rows are selected when i click a row.
Thanks in advance
This is because UITableView dequeues the cell. As you are showing 10 rows per screen it totally fits, that row 11 appears checked as it is dequeued from row 1.
To avoid that behavior you have to add a check to tableView:cellForRowAtIndexPath: to set your checked parameter for each cell – like the text you set for every cell.
Try this:
NSArray *arrayNibs = [[NSBundle mainBundle] loadNibNamed:#"InterestsTableViewCell" owner:self options:nil];
for (id currentObject in arrayNibs){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (InterestsTableViewCell *) currentObject;
cell.Delegate = self;
cell.total = [dataArray count];
break;
}
}
and let me know if this works for you.
Quick Fix - change this line from
InterestsTableViewCell *cell = (InterestsTableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
to
InterestsTableViewCell *cell = nil;
Its not good from a memory/efficiency perspective because you won't be reusing the cells, instead will be reloading one from nib each time - but it will demonstrate whether what Florian Mielke is saying is correct (and his recommended approach will provide a better solution long term).