How to improve UITableView scrolling performance? What I am I missing? - ios

I know the basics, what not to do in cellForRowAtIndexPath:, that may cause scrolling performance to be hindered. And I believe I have followed those rules, which is why I have gotten this far. My UITableView is horrible at scrolling, does so very well, but there are times where it stutters for split seconds to seconds, noticeable when it starts to slow down a bit.
Without revealing too much of my code, what here am I doing that could be causing this. I think I have gone over everything and eliminated something that could be causing it, but the problem persists. I feel as if it is something that is obvious and I am overlooking. Help is tremendously appreciated.
Thank you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"tweetCell";
TweetCell *cell = (TweetCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
NSDictionary *tweet = _tweets[indexPath.row];
NSString *username = tweet[#"username"];
CGFloat tweetHeight = [tweet[#"contentHeight"] floatValue];
cell.tweet.frame = ({
CGRect frame = cell.tweet.frame;
frame.size.height = tweetHeight + 2;
frame.origin.y = cell.bounds.size.height / 2 - frame.size.height / 2;
frame;
});
cell.tweet.attributedText = tweet[#"attributedText"];
cell.imageView.image = [_profilePhotos[username] valueForKey:#"image"];
cell.date.text = tweet[#"dateString"];
if (tweet[#"media"]) {
cell.tweetImage.image = tweet[#"media"];
cell.tweetImage.hidden = NO;
} else {
cell.tweetImage.image = nil;
cell.tweetImage.hidden = YES;
}
NSMutableAttributedString *attributedText = [tweet[#"attributedText"] mutableCopy];
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"useDynamicTextSize"]) {
UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
if (cell.tweet.font.pointSize != font.pointSize) {
cell.tweet.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:font.pointSize];
cell.username.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:font.pointSize];
cell.date.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:font.pointSize];
[attributedText addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Light" size:font.pointSize] range:NSMakeRange(0, attributedText.length)];
} else {
[attributedText addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Light" size:cell.tweet.font.pointSize] range:NSMakeRange(0, attributedText.length)];
}
} else {
[attributedText addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Light" size:17] range:NSMakeRange(0, attributedText.length)];
}
cell.tweet.attributedText = attributedText;
cell.username.adjustsFontSizeToFitWidth = YES;
NSString *color = [[NSUserDefaults standardUserDefaults] objectForKey:#"color"];
if ([color isEqualToString:#"automatic"]) {
color = ([[UIScreen mainScreen] brightness] <= .5) ? #"black" : #"white";
}
[cell.imageView.layer setMasksToBounds:YES];
[cell.imageView.layer setCornerRadius:5];
[cell.imageView.layer setBorderColor:[[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.55] CGColor]];
[cell.imageView.layer setBorderWidth:0.5];
[cell.tweetImage.layer setMasksToBounds:YES];
[cell.tweetImage.layer setCornerRadius:5];
[cell.tweetImage.layer setBorderColor:[[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.55] CGColor]];
[cell.tweetImage.layer setBorderWidth:0.65];
// [UIView beginAnimations:nil context:nil];
// [UIView setAnimationDuration:0.25];
// [UIView setAnimationDelegate:self];
if ([color isEqualToString: #"white"]) {
cell.tweet.textColor =[UIColor blackColor];
cell.date.textColor = [UIColor blackColor];
cell.username.textColor = [UIColor blackColor];
cell.backgroundColor = [UIColor whiteColor];
cell.tweet.linkTextAttributes = #{NSForegroundColorAttributeName:[UIColor blueColor]};
[tweet[#"attributedText"] addAttribute:NSForegroundColorAttributeName
value:[UIColor blackColor]
range:NSMakeRange(0, cell.tweet.attributedText.length)];
} else /*if ([color isEqualToString: #"black"])*/ {
cell.tweet.textColor = [UIColor whiteColor];
cell.date.textColor = [UIColor whiteColor];
cell.username.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor colorWithRed:52/255.0 green:52/255.0 blue:52/255.0 alpha:1];
cell.tweet.linkTextAttributes = #{NSForegroundColorAttributeName: [UIColor colorWithRed:0.66 green:0.82 blue:1 alpha:1]};
[tweet[#"attributedText"] addAttribute:NSForegroundColorAttributeName
value:[UIColor whiteColor]
range:NSMakeRange(0, cell.tweet.attributedText.length)];
}
// [UIView commitAnimations];
if (_retweets[_tweets[indexPath.row][#"id"]]) {
if ([color isEqualToString: #"white"]) {
cell.backgroundColor = [UIColor colorWithRed:0.945 green:0.945 blue:0.945 alpha:1];
} else if ([color isEqualToString: #"black"]) {
cell.backgroundColor = [UIColor colorWithRed:0.114 green:0.114 blue:0.114 alpha:1];
}
// cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, cell.bounds.size.width);
} else {
if ([color isEqualToString: #"white"]) {
[cell setBackgroundColor:[UIColor whiteColor]];
} else if ([color isEqualToString: #"black"]) {
cell.backgroundColor = [UIColor colorWithRed:52/255.0 green:52/255.0 blue:52/255.0 alpha:1];
}
// cell.separatorInset = UIEdgeInsetsMake(0, 80, 0, 0);
}
cell.tweet.tag = indexPath.row;
cell.tweetImage.userInteractionEnabled = YES;
cell.tweet.frame = ({
CGRect frame = cell.tweet.frame;
frame.size.height = tweetHeight + 2;
frame.origin.y = cell.bounds.size.height / 2 - frame.size.height / 2;
if (tweet[#"media"]) {
frame.size.width = 164;
} else {
frame.size.width = 224;
}
frame;
});
cell.tweet.delegate = self;
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(retweetTweet:)];
doubleTap.numberOfTapsRequired = 2;
[cell addGestureRecognizer:doubleTap];
[cell.tweet addGestureRecognizer:doubleTap];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showImage:)];
tap.numberOfTapsRequired = 1;
[cell.tweetImage addGestureRecognizer:tap];
UITapGestureRecognizer *tapToViewProfile = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(viewProfile:)];
tap.numberOfTapsRequired = 1;
[cell.imageView setUserInteractionEnabled:YES];
[cell.imageView addGestureRecognizer:tapToViewProfile];
return cell;
}
TweetCell.m:
//
// TweetCell.m
// Khabara
//
// Created by Isa Ranjha on 3/26/14.
// Copyright (c) 2014 Isa Ranjha. All rights reserved.
//
#import "TweetCell.h"
#implementation TweetCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
//self.imageView.frame = CGRectMake(self.imageView.frame.origin.x,self.imageView.frame.origin.y,45,45);
self.imageView.frame = ({
CGRect frame = self.imageView.frame;
frame.size = CGSizeMake(48, 48);
frame.origin.y = self.bounds.size.height / 2 - frame.size.height / 2;
frame;
});
self.imageView.backgroundColor = [UIColor whiteColor];
}
- (void)awakeFromNib
{
//_tweet.numberOfLines = 0;
_tweet.textContainerInset = UIEdgeInsetsZero;
_tweet.canCancelContentTouches = YES;
// _tweet.autoresizingMask = (UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end

Do not use the set layer corner radius on cell. It make the problem. Instead of this, you better create a frame .png image.

Related

not able to tap on MKPinAnnotationView Second Time

I have created Custom annotation by using this code.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *const AnnotatioViewReuseID = #"AnnotatioViewReuseID";
// MKAnnotationView
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotatioViewReuseID];
if (!annotationView) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotatioViewReuseID];
}
if ([annotation isKindOfClass:[FBAnnotationCluster class]]) {
FBAnnotationCluster *cluster = (FBAnnotationCluster *)annotation;
cluster.title = [NSString stringWithFormat:#"%lu", (unsigned long)cluster.annotations.count];
UIView *view = [[UIView alloc]init];
view.backgroundColor = [UIColor colorWithRed:33.0/255.0 green:191.0/255.0 blue:133.0/255.0 alpha:1.0];
UILabel *label = [[UILabel alloc]init];
label.text = cluster.title;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
UIFont *font = [UIFont fontWithName:#"Avenir-Medium" size:14.0];
label.font = font;
label.frame = CGRectMake(0, 0, [self widthOfString:label.text]+20, [self widthOfString:label.text]+20);
view.frame = CGRectMake(0, 0, label.frame.size.width, label.frame.size.width) ;
view.layer.cornerRadius = view.frame.size.height/2;
view.layer.borderColor = [UIColor whiteColor].CGColor;
view.layer.borderWidth = 2.0;
view.clipsToBounds = true;
[view addSubview:label];
for (UIView *view in [annotationView subviews])
{
[view removeFromSuperview];
}
[annotationView addSubview:view];
annotationView.enabled = YES;
annotationView.annotation = annotation;
annotationView.canShowCallout = YES;
annotationView.pinTintColor = [UIColor clearColor];
} else {
annotationView.pinTintColor = [UIColor clearColor];
annotationView.layer.borderColor = [UIColor clearColor].CGColor;
annotationView.layer.borderWidth = 0.0;
FBAnnotation *a = (FBAnnotation*)annotation;
NSLog(#"amount is %f",a.amount);
UIImage *image = [UIImage imageNamed:#"icon-marker-select"];
UIImageView *imgView = [[UIImageView alloc]init];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
formatter.maximumFractionDigits = 2;
NSString *result = [formatter stringFromNumber:[NSNumber numberWithDouble:a.amount]];
NSString *strData = [NSString stringWithFormat:#"%#%#",a.currency,result];
UIFont *font = [UIFont fontWithName:#"Avenir-Medium" size:14.0];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = #{
NSFontAttributeName : font,
NSParagraphStyleAttributeName : paragraphStyle,
NSForegroundColorAttributeName : [UIColor whiteColor]
};
CGSize textSize = [strData sizeWithAttributes:attributes];
CGRect textRect = CGRectMake(5, (image.size.height-textSize.height)/2 - 2, textSize.width , textSize.height);
UILabel *textLable = [[UILabel alloc]initWithFrame:textRect];
textLable.textColor = [UIColor whiteColor];
textLable.font = font;
textLable.text = strData;
UIImage *lightSymImg = [UIImage imageNamed:#"icon_lightning"];
UIImageView *lightImage = [[UIImageView alloc]init];
lightImage.image = lightSymImg;
CGRect imgRect = CGRectMake(textRect.origin.x+textRect.size.width, (image.size.height-lightSymImg.size.height)/2 - 2, lightSymImg.size.width,lightSymImg.size.height);
lightImage.frame = imgRect;
imgView.frame = CGRectMake(0, 0, imgRect.size.width + imgRect.origin.x+5, image.size.height);
imgView.image = image;
for (UIView *view in [annotationView subviews])
{
[view removeFromSuperview];
}
annotationView.annotation = annotation;
[annotationView addSubview:imgView];
[annotationView addSubview:textLable];
[annotationView addSubview:lightImage];
annotationView.enabled = YES;
annotationView.canShowCallout = NO;
}
return annotationView;
}
And in didSelect method i have written this code for Present a view Controller
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
NSLog(#"didSelectAnnotationView");
if ([view.annotation isKindOfClass:[FBAnnotationCluster class]]) {
NSLog(#"FBAnnotationCluster select annotation");
}
else{
SpotDetailVC *locDetailVC = [[UIStoryboard storyboardWithName:#"SpotDetail" bundle:nil] instantiateViewControllerWithIdentifier:#"SpotDetailVC"];
[self presentViewController:navController animated:YES completion:nil];
}
}
Now When i click on annotation for the first Time it works but after dismiss that view again click on Same annotation it does not works.
Please help, i know it's Silly mistake but i am not able to configure out that one.

UITextField will not become first responder when touched, none of the usual mistakes apply

I have a UITextField that will not become the first responder when tapped. I can assign it to become first responder, which works find. But if it resigns first responder status and I try and tap it or tab back to it to make it become the first responder again, nothing happens. It appears as if the touch is being trapped somewhere, and yet I can't find anything in my code that could be causing that to happen. I've checked the usual suspects:
If the textfield the top view
is the textfield within the bounds of it's superview
is the textfield userEnabled.
I've also rewritten the code in several different ways, to no avail.
Can anyone help me with this problem. The textField at issue is the field titled answerTextField in the method createOneDoubleViewAtOrigin.
The relevant code is below.
-(instancetype)initForProblem:(NSString *)problem{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
self = [super init];
if (self) {
[self parseBasicFractionProblem:problem];
if (_problemType == fractDoubleWithPic) {
NSLog(#"placed the fract views");
UIView *firstProblemView = [self createOneDoubleViewAtOrigin:CGPointMake(26, 30) withNumerator:_numerator1 denominator:_denominator1 forViewNumber:0];
UIView *secondProblemView = [self createOneDoubleViewAtOrigin:CGPointMake(342,30) withNumerator:_numerator2 denominator:_denominator2 forViewNumber:1];
[self addSubview:firstProblemView];
[self addSubview:secondProblemView];
[self bringSubviewToFront:firstProblemView];
[self bringSubviewToFront:secondProblemView];
}
else if (_problemType == fractDoubleNoPicAns||_problemType == fractDoubleNoPicExtendedAns ){
}
}
self.tag = 800;
self.backgroundColor = [UIColor redColor];
NSLog(#"made to end");
return self;
}
-(UIView *)createOneDoubleViewAtOrigin:(CGPoint)viewOrigin withNumerator:(NSInteger)numerator denominator:(NSInteger)denominator forViewNumber:(NSInteger)viewNumber{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
UIView *containerView = [[UIView alloc] initWithFrame: CGRectMake(viewOrigin.x,viewOrigin.y, 310, 263)];
containerView.backgroundColor = [UIColor colorWithRed:178.0/255.0 green:222.0/255.0 blue:80.0/255.0 alpha:1.0];
containerView.layer.cornerRadius = 5.0;
UILabel *numeratorView = [self createSubview:CGRectMake(66, 23, 59, 47) text:[NSString stringWithFormat:#"%ld",(long)numerator] inView:containerView];
UILabel *divisorView = [self createSubview:CGRectMake(66, 40, 59, 47) text:#"___" inView:containerView];
UILabel *denominatorView = [self createSubview:CGRectMake(72, 82, 47, 47) text:[NSString stringWithFormat:#"%ld",(long)denominator] inView:containerView];
UILabel *equals = [self createSubview:CGRectMake(125, 50, 47, 47) text:#"=" inView:containerView];
/*
FFractSupportedTextField *answerField = [self createAnswerField:CGRectMake(173,50,82,47)];
*/
UITextField *answerTextField = [[UITextField alloc] initWithFrame:CGRectMake(173,50,82,47)];
//Inside
answerTextField.font = [UIFont fontWithName:#"Helvetica" size:30.0];
answerTextField.textAlignment = NSTextAlignmentCenter;
answerTextField.placeholder = #"?";
//border
answerTextField.layer.borderWidth = 1;
answerTextField.layer.borderColor = [[UIColor blackColor] CGColor];
answerTextField.layer.cornerRadius = 5.0;
answerTextField.userInteractionEnabled = YES;
[answerTextField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
containerView.tag = 820 + 6*viewNumber;
numeratorView.tag = 821 + 6*viewNumber;
divisorView.tag = 822 + 6*viewNumber;
denominatorView.tag = 823 + 6*viewNumber;
equals.tag = 824 + 6*viewNumber;
answerTextField.tag = 801 + viewNumber;
UIView *pictureView = [self createFractPictureForNumerator:numerator denominator:denominator number:viewNumber];
pictureView.tag = 825 + 6*viewNumber;
if (viewNumber == 0){
_answerTextField1 = answerTextField;
[containerView addSubview:_answerTextField1];
[containerView bringSubviewToFront:_answerTextField1];
_pictureView1 = pictureView;
[containerView addSubview:_pictureView1];
[_answerTextField1 becomeFirstResponder];
} else if (viewNumber == 1) {
_answerTextField2 = answerTextField;
[containerView addSubview:_answerTextField2];
[containerView bringSubviewToFront:_answerTextField2];
_pictureView2 = pictureView;
[containerView addSubview:_pictureView2];
}
return containerView;
}
-(UILabel *)createSubview:(CGRect)frame text:(NSString *)text inView:(UIView *)containerView{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
UILabel *labelView = [[UILabel alloc] initWithFrame:frame];
labelView.font = [UIFont fontWithName:#"Helvetica" size:30.0];
labelView.textAlignment = NSTextAlignmentCenter;
labelView.text = text;
[containerView addSubview:labelView];
return labelView;
}
-(FFractSupportedTextField *)createAnswerField:(CGRect)frame{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
FFractSupportedTextField *fieldView = [[FFractSupportedTextField alloc] initWithFrame:frame];
//Inside
fieldView.font = [UIFont fontWithName:#"Helvetica" size:30.0];
fieldView.textAlignment = NSTextAlignmentCenter;
fieldView.placeholder = #"?";
//border
fieldView.layer.borderWidth = 1;
fieldView.layer.borderColor = [[UIColor blackColor] CGColor];
fieldView.layer.cornerRadius = 5.0;
fieldView.userInteractionEnabled = YES;
return fieldView;
}
-(UIView *)createFractPictureForNumerator:(NSInteger)numerator denominator:(NSInteger)denominator number:(NSInteger)viewNumber{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
NSLog(#"numerator:%ld denominator:%ld",(long)numerator,(long)denominator);
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(33, 165, 256, 78)];
containerView.backgroundColor = [UIColor whiteColor];
containerView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
containerView.layer.borderWidth = 1.0;
containerView.layer.cornerRadius = 3.0;
NSInteger smallViewCount = denominator;
if (denominator == 0) {
smallViewCount = 1;
}
float smallWidth = 245.0/smallViewCount;
for (int n = 0; n < smallViewCount; n++) {
NSLog(#"count %d",n);
UILabel *smallLabel = [[UILabel alloc] initWithFrame:CGRectMake(8 + n*smallWidth, 8, smallWidth - 5, 29)];
smallLabel.backgroundColor = [UIColor colorWithRed:195.0/255.0 green:222.0/255.0 blue:172.0/255.0 alpha:1.0];
smallLabel.font = [UIFont fontWithName:#"Helvetica" size:17.0];
[smallLabel setAdjustsFontSizeToFitWidth:YES];
smallLabel.textAlignment = NSTextAlignmentCenter;
smallLabel.layer.cornerRadius = 3.0;
smallLabel.tag = 830+n + viewNumber*10;
[containerView addSubview:smallLabel];
}
UILabel *largeLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 41, 240, 29)];
largeLabel.backgroundColor = [UIColor colorWithRed:195.0/255.0 green:222.0/255.0 blue:172.0/255.0 alpha:1.0];
largeLabel.text = [NSString stringWithFormat:#"= %ld",(long)numerator];
largeLabel.textAlignment = NSTextAlignmentCenter;
largeLabel.layer.cornerRadius = 3.0;
[containerView addSubview:largeLabel];
NSLog(#"end of createFractPictFor..");
return containerView;
}

How to set Badge value based on NextviewControllerValue?

I want to populate the firstComponentValue in a lbl_card_count badge.
UILabel *lbl_card_count = [[UILabel alloc]initWithFrame:CGRectMake(23,0, 13, 13)];
int Temp_card_count;
lbl_card_count.textColor = [UIColor whiteColor];
lbl_card_count.textAlignment = NSTextAlignmentCenter;
lbl_card_count.text = [NSString stringWithFormat:#"%d",Temp_card_count];
lbl_card_count.layer.borderWidth = 1;
lbl_card_count.layer.cornerRadius = 8;
lbl_card_count.layer.masksToBounds = YES;
lbl_card_count.layer.borderColor =[[UIColor clearColor] CGColor];
lbl_card_count.layer.shadowColor = [[UIColor clearColor] CGColor];
lbl_card_count.layer.shadowOffset = CGSizeMake(0.0, 0.0);
lbl_card_count.layer.shadowOpacity = 0.0;
lbl_card_count.backgroundColor = [UIColor colorWithRed:247.0/255.0 green:45.0/255.0 blue:143.0/255.0 alpha:1.0];
lbl_card_count.font = [UIFont fontWithName:#"ArialMT" size:11];
[lbl_card_count setHidden:YES];
[appliancesButton addSubview:lbl_card_count];
[categoryView addSubview:appliancesButton];
-(void)changecolor:(UIGestureRecognizer *)gestureRecognizer
{
AddApplianceViewController *addApplianceVC = [[AddApplianceViewController alloc]init];
[self showPopupWithTransitionStyle:STPopupTransitionStyleSlideVertical rootViewController:addApplianceVC];
addApplianceVC.labelText = gestureRecognizer.accessibilityLabel;
NSLog(#"tapped");
}
AddAppliancesViewController.m
-(void)setButtonAction
{
if ([self.picker selectedRowInComponent:0]!=0)
{
firstComponentValue = [self.picker selectedRowInComponent:0];
NSLog(#"Appliances Count==>>%d",firstComponentValue);
[self dismissViewControllerAnimated:YES completion:NO];
}
else
{
[self alert];
}
}`

How to add padding from left right only for label in uitable cell

How to add padding from left right only for label in UITableViewCell?
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"cellmessage";
SendMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[SendMessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell setAccessoryType:UITableViewCellAccessoryNone];
NSDictionary *dict = [self->serverArray objectAtIndex: indexPath.row];
cell.celllab.text= [dict objectForKey:#"message"];
CGFloat fixedWidth = cell.celllab.frame.size.width;
CGSize newSize = [cell.celllab sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = cell.celllab.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height + 10);
cell.celllab.frame = newFrame;
cell.celllab.layer.cornerRadius = 4.0f;
cell.celllab.layer.masksToBounds = YES;
//cell.celllab.layer.borderColor = [UIColor blackColor].CGColor;
//cell.celllab.layer.borderWidth = 10.0;
CAShapeLayer *shape = [CAShapeLayer layer];
shape.frame = cell.celllab.bounds;
//shape.path = maskPath.CGPath;
shape.lineWidth = 3.0f;
shape.strokeColor = [UIColor whiteColor].CGColor;
[cell.celllab.layer addSublayer:shape];
if([[dict objectForKey:#"type"] isEqualToString:#"u2a"])
{
cell.celllab.textAlignment = NSTextAlignmentRight;
cell.celllab.backgroundColor = [UIColor colorWithRed:0.7 green:0.91 blue:0.26 alpha:1];
cell.celllab.textColor = [UIColor blackColor];
cell.blackarrow.hidden = YES;
cell.greenarrow.hidden = NO;
cell.bmwatch.hidden = YES;
cell.swiliam.hidden = NO;
cell.greenmsg.hidden = NO;
cell.whitemsg.hidden = YES;
}
else
{
cell.celllab.backgroundColor = [UIColor blackColor];
cell.celllab.textColor = [UIColor whiteColor];
cell.celllab.textAlignment = NSTextAlignmentLeft;
cell.greenarrow.hidden = YES;
cell.blackarrow.hidden = NO;
cell.bmwatch.hidden = NO;
cell.swiliam.hidden = YES;
cell.greenmsg.hidden = YES;
cell.whitemsg.hidden = NO;
}
return cell;
}
In your SendMessageTableViewCell class write below function to change the frames of its subviews
- (void)layoutSubviews{
self.textLabel.frame = CGRectMake(rightPadding, topPadding,self.frame.size.width - (2*rightPadding), self.frame.size.height-(2*topPadding))
}
Above code in your custom class will be called when your cell contents subviews changes their frame and at that time you can give padding to your lable and also can change other contents frames.
Its very simple.Just subclass the uilabel and write this method
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 5, 0, 5};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}

Slow loading tableview on iPhone with reusable cells

I load the data from Parse.com backend, they send me a solutions for use the reusable cells but now I still have troubles with the loading speed, this is the coding I have in my tableview and I have a Subclass for making up my cells (ExploreStreamCustomCell.m)
- (ExploreStreamCustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
object:(PFObject *)object
{
static NSString *CellIdentifier = #"ExploreStreamCustomCell";
ExploreStreamCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ExploreStreamCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}
// Configure the cell
cell.listItemTitle.text = [object objectForKey:#"text"];
cell.checkinsLabel.text = [NSString stringWithFormat:#"%#", [object objectForKey:#"checkins"]];
cell.descriptionLabel.text = [object objectForKey:#"description"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
PFFile *listThumbnail = [object objectForKey:#"header"];
cell.listViewImage.image = [UIImage imageNamed:#"loading_image_stream.png"]; // placeholder image
cell.listViewImage.file = listThumbnail;
[cell.listViewImage loadInBackground:NULL];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForNextPageAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [super tableView:tableView cellForNextPageAtIndexPath:indexPath];
cell.textLabel.font = [cell.textLabel.font fontWithSize:kPAWWallPostTableViewFontSize];
return cell;
}
If I have all the content of the //configure cell in the cell == nil the it's fast but it show up 3 of the 9 unique datarows and repeat those 3 unique content cell 3 times?
Edit extra code within ExploreStreamCustomCell.m
#import "ExploreStreamCustomCell.h"
#implementation ExploreStreamCustomCell
#synthesize listViewImage,
iconLocation,
iconPeople,
iconCheckins,
listItemTitle,
locationLabel,
peopleLabel,
checkinsLabel,
descriptionLabel,
listItemView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
//Initialization code
listItemView = [[UIView alloc] init];
listViewImage = [[PFImageView alloc] init];
iconLocation = [[UIImageView alloc] init];
iconPeople = [[UIImageView alloc] init];
iconCheckins = [[UIImageView alloc] init];
listItemTitle = [[UILabel alloc] init];
locationLabel = [[UILabel alloc] init];
peopleLabel = [[UILabel alloc] init];
checkinsLabel = [[UILabel alloc] init];
descriptionLabel = [[UILabel alloc] init];
listViewImage.image = [UIImage imageNamed:#"nachtwacht_list_formaat.png"];
iconLocation.image = [UIImage imageNamed:#"icon_magenta_location.png"];
iconPeople.image = [UIImage imageNamed:#"icon_magenta_people.png"];
iconCheckins.image = [UIImage imageNamed:#"icon_magenta_checkins.png"];
listItemTitle.text = #"text";
locationLabel.text = #"0,7 km";
peopleLabel.text = #"34";
checkinsLabel.text = #"61";
descriptionLabel.text = #"Description text.";
[self.contentView addSubview:listItemView];
[self.contentView addSubview:listViewImage];
[self.contentView addSubview:iconLocation];
[self.contentView addSubview:iconPeople];
[self.contentView addSubview:iconCheckins];
[self.contentView addSubview:listItemTitle];
[self.contentView addSubview:locationLabel];
[self.contentView addSubview:peopleLabel];
[self.contentView addSubview:checkinsLabel];
[self.contentView addSubview:descriptionLabel];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+0 , 33, 280, 124);
listViewImage.frame = frame;
listViewImage.contentMode = UIViewContentModeScaleAspectFill;
listViewImage.layer.masksToBounds = YES;
//listViewImage.backgroundColor = [UIColor lightGrayColor];
frame= CGRectMake(boundsX+20 , 164, 12, 18);
iconLocation.frame = frame;
//iconLocation.backgroundColor = [UIColor lightGrayColor];
frame= CGRectMake(boundsX+102 , 164, 24, 18);
iconPeople.frame = frame;
//iconPeople.backgroundColor = [UIColor lightGrayColor];
frame= CGRectMake(boundsX+193 , 164, 20, 16);
iconCheckins.frame = frame;
//iconLocation.backgroundColor = [UIColor lightGrayColor];
frame= CGRectMake(boundsX+0 , 0, 280, 33);
listItemView.frame = frame;
listItemView.backgroundColor = [UIColor colorWithRed:0.749 green:0.000 blue:0.243 alpha:1.000];
frame= CGRectMake(boundsX+20 , 3, 240, 29);
listItemTitle.frame = frame;
//listItemTitle.textColor = [UIColor colorWithRed:250.0f/255.0f green:194.0f/255.0f blue:9.0f/255.0f alpha:0.8f];
listItemTitle.textAlignment = UITextAlignmentLeft;
listItemTitle.font = [UIFont boldSystemFontOfSize:15];
listItemTitle.textColor = [UIColor whiteColor];
listItemTitle.backgroundColor = [UIColor clearColor];
listItemTitle.lineBreakMode = UILineBreakModeTailTruncation;
//listItemTitle.backgroundColor = [UIColor orangeColor];
frame= CGRectMake(boundsX+40 , 164, 57, 21);
locationLabel.frame = frame;
locationLabel.textAlignment = UITextAlignmentLeft;
locationLabel.font = [UIFont boldSystemFontOfSize:12];
locationLabel.textColor = [UIColor colorWithRed:0.749 green:0.000 blue:0.243 alpha:1.000];
locationLabel.backgroundColor = [UIColor clearColor];
locationLabel.lineBreakMode = UILineBreakModeTailTruncation;
locationLabel.numberOfLines = 1;
//locationLabel.backgroundColor = [UIColor redColor];
frame= CGRectMake(boundsX+134 , 164, 57, 21);
peopleLabel.frame = frame;
peopleLabel.textAlignment = UITextAlignmentLeft;
peopleLabel.font = [UIFont boldSystemFontOfSize:12];
peopleLabel.textColor = [UIColor colorWithRed:0.749 green:0.000 blue:0.243 alpha:1.000];
peopleLabel.backgroundColor = [UIColor clearColor];
peopleLabel.lineBreakMode = UILineBreakModeTailTruncation;
peopleLabel.numberOfLines = 1;
frame= CGRectMake(boundsX+221 , 164, 51, 21);
checkinsLabel.frame = frame;
checkinsLabel.textAlignment = UITextAlignmentLeft;
checkinsLabel.font = [UIFont boldSystemFontOfSize:12];
checkinsLabel.textColor = [UIColor colorWithRed:0.749 green:0.000 blue:0.243 alpha:1.000];
checkinsLabel.backgroundColor = [UIColor clearColor];
checkinsLabel.lineBreakMode = UILineBreakModeTailTruncation;
checkinsLabel.numberOfLines = 1;
frame= CGRectMake(boundsX+0 , 189, 280, 55);
descriptionLabel.frame = frame;
descriptionLabel.textAlignment = UITextAlignmentLeft;
descriptionLabel.font = [UIFont systemFontOfSize:13];
descriptionLabel.backgroundColor = [UIColor clearColor];
descriptionLabel.lineBreakMode = UILineBreakModeTailTruncation;
descriptionLabel.numberOfLines = 3;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
/*
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
A great way to explore the inefficiencies of your code is to use Instruments' Time Profiler tool. The Time Profiler will let you see how much time is being spent on each task, line-by-line in your code.
I would recommend the following settings for profiling:
From Apple's Face Detection sample app:
You can then double click any line (higher percentages mean more time is being devoted to that method call) to see in the code how much time is spent in each place.
From here you can begin to figure out where you are being inefficient and see exactly what is taking up so much time. Good luck!

Resources