UICollectionView ui label not displaying - ios

I am new to iOS development and this is my first project that i am doing alone. i am trying to show the feed of my college's unofficial fb page using uicollectionview. I have been trying a lot of different things but nothing seems to work. The uilabel for the name is not being added to the collection view. However before i added the sizeForLabelAtIndexPath method which is to determine the size of the 2 ui labels and the height of the image to be displayed, the name label was working fine
UICollectionViewController.m file
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"DCE Speaks Up";
self.collectionView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = NO;
NSError *error= nil;
NSURL *feedURL = [NSURL URLWithString:#"https://graph.facebook.com/382057938566656/feed?fields=id,full_picture,message,story,created_time,link&access_token=1750413825187852%7CkUl9nlZFPvdGxGZX4WYabKKG2G4"];
NSData *jsonData = [NSData dataWithContentsOfURL:feedURL];
NSDictionary *dataDictionary= [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
self.feedArray = [NSMutableArray array];
NSArray *feedTempArray = [dataDictionary objectForKey:#"data"]; //feedTempArray just used for parsing
for (NSDictionary *feedDictionary in feedTempArray) {
FacebookFeed *fbFeed =[FacebookFeed facebookFeedWithMessage:[feedDictionary objectForKey:#"message"]];
fbFeed.story = [feedDictionary objectForKey:#"story"];
fbFeed.imageURL = [NSURL URLWithString:[feedDictionary objectForKey:#"full_picture"]];
fbFeed.date = [feedDictionary objectForKey:#"created_time"];
fbFeed.sharedLink = [NSURL URLWithString:[feedDictionary objectForKey:#"link"]];
[self.feedArray addObject:fbFeed];
}
// Register cell classes
[self.collectionView registerClass:[FacebookCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
// Do any additional setup after loading the view.
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
FacebookCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
cell.feed = self.feedArray[indexPath.row];
cell.sizeDictionary = [self sizeForLabelAtIndexPath:indexPath.row collectionView:collectionView];
// Configure the cell
for (UIView *view in cell.contentView.subviews) {
if ([view isKindOfClass:[UILabel class]]) {
[view removeFromSuperview];
}else if ([view isKindOfClass:[UIImageView class]]){
[view removeFromSuperview];
}
}
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
return cell;
}
#pragma mark <UICollectionViewDelegateFlowLayout>
-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *sizeDictionary = [[NSDictionary alloc] init];
sizeDictionary = [self sizeForLabelAtIndexPath:indexPath.row collectionView:collectionView];
return CGSizeMake(self.view.frame.size.width - 20, [sizeDictionary[#"imageHeight"] floatValue] + [sizeDictionary[#"messageHeight"] floatValue] + [sizeDictionary[#"nameHeight"] floatValue]);
}
#pragma mark <UICollectionViewDelegate>
- (NSDictionary *) sizeForLabelAtIndexPath:(NSUInteger)indexPath collectionView:(UICollectionView *)collectionView{
FacebookFeed *feed = self.feedArray[indexPath];
CGSize nameSize = CGSizeFromString(#"DCE Speaks Up");
CGSize imageSize = CGSizeMake(0, 0);
if (feed.imageURL) {
imageSize = CGSizeMake(470, 394);
}
float height = 80;
NSString *string = #"";
if (feed.message) {
string = [feed.message stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}else if (feed.story){
string = [feed.story stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
if (string) {
NSDictionary *attributes = #{
NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleBody]
};
CGRect bodyFrame =
[string boundingRectWithSize:CGSizeMake(CGRectGetWidth(collectionView.bounds),
CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin)
attributes:attributes
context:nil];
height += ceilf(CGRectGetHeight(bodyFrame));
}
return #{ #"imageHeight" : #(imageSize.height) , #"messageHeight" : #(height) , #"nameHeight" : #(nameSize.height)};
}
Custom cell file
#implementation FacebookCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self)
{
self.contentView.backgroundColor = [UIColor whiteColor];
// change to our custom selected background view
self.imageView = [[UIImageView alloc] init];
self.messageLabel = [[UILabel alloc] init];
self.nameLabel = [[UILabel alloc] init];
}
return self;
}
-(void) setFeed:(FacebookFeed *)feed{
_feed = feed;
self.messageLabel.numberOfLines = 0;
self.messageLabel.lineBreakMode = 0;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data= [NSData dataWithContentsOfURL:feed.imageURL];
UIImage *theImage=[UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image= theImage ;
});
});
if (feed.message) {
self.messageLabel.text = [[NSString alloc] initWithString:[feed.message stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}else{
self.messageLabel.text = [[NSString alloc] initWithString:[feed.story stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
self.messageLabel.font = [UIFont fontWithName:#"ArialMT" size:11];
[self.messageLabel sizeToFit];
self.nameLabel.text = #"DCE Speaks Up";
[self.nameLabel setFont:[UIFont fontWithName:#"ArialMT" size:12]];
[self.nameLabel sizeToFit];
}
-(void) layoutSubviews{
NSLog(#"%#",self.feed);
self.nameLabel.frame = CGRectMake(10, 10, self.contentView.bounds.size.width, [self.sizeDictionary[#"nameHeight"] floatValue]);
float height = 200;
if (self.contentView.bounds.size.height - [self.sizeDictionary[#"messageHeight"] floatValue] -[self.sizeDictionary[#"nameHeight"] floatValue] >100) {
height = self.contentView.bounds.size.height - [self.sizeDictionary[#"messageHeight"] floatValue] -[self.sizeDictionary[#"nameHeight"] floatValue];
}
if (self.feed.imageURL) {
self.imageView.frame = CGRectMake(10, [self.sizeDictionary[#"messageHeight"] floatValue] +[self.sizeDictionary[#"nameHeight"] floatValue], self.contentView.bounds.size.width - 20,height);
self.messageLabel.frame = CGRectMake(10, [self.sizeDictionary[#"nameHeight"] floatValue] +10, self.contentView.bounds.size.width - 10, [self.sizeDictionary[#"messageHeight"] floatValue]);
[self.contentView addSubview:self.messageLabel];
[self.contentView addSubview:self.imageView];
}else{
self.messageLabel.frame = CGRectMake(10, [self.sizeDictionary[#"nameHeight"] floatValue], self.contentView.bounds.size.width - 10, [self.sizeDictionary[#"messageHeight"] floatValue]);
[self.contentView addSubview:self.messageLabel];
}
[self.contentView addSubview:self.nameLabel];
}
#end

Related

Add UIControl programmatically

I try a adding UITableView into scrollview programmatically. I have set anchors and datasource, delegate. You can see in codes. But I can't. I put a breakpoint and everything run. But I can't see in my scrollview.
UITableView *tableView = [[UITableView alloc] init];
tableView.rowHeight = 130;
tableView.translatesAutoresizingMaskIntoConstraints = false;
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = [UIColor clearColor];
[self.scrollView addSubview:tableView];
[tableView.leftAnchor constraintEqualToAnchor:self.scrollView.leftAnchor constant:8].active = YES;
[tableView.rightAnchor constraintEqualToAnchor:self.scrollView.rightAnchor constant:8].active = YES;
[tableView.topAnchor constraintEqualToAnchor:self.viewShareBasketExtra.topAnchor constant:8].active = YES;
[tableView reloadData];
This codes are working:
UILabel *lblPrice = [[UILabel alloc] init];
[lblPrice setFont:[UIFont fontWithName:#"HelveticaNeue" size:14]];
lblPrice.translatesAutoresizingMaskIntoConstraints = false;
[lblPrice setTextAlignment:NSTextAlignmentCenter];
lblPrice.text = [NSString stringWithFormat:#"%.2f TL",[_productModel Price]];
[self.priceView addSubview:lblPrice];
[lblPrice.leftAnchor constraintEqualToAnchor:self.priceView.leftAnchor constant:8].active = YES;
[lblPrice.rightAnchor constraintEqualToAnchor:self.priceView.rightAnchor constant:8].active = YES;
[lblPrice.centerXAnchor constraintEqualToAnchor:self.priceView.centerXAnchor].active = YES;
[lblPrice.centerYAnchor constraintEqualToAnchor:self.priceView.centerYAnchor].active = YES;
[self getComments];
But that codes are not working :
UITableView *tableView = [[UITableView alloc]init];
tableView.rowHeight = 130;
tableView.translatesAutoresizingMaskIntoConstraints = false;
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = [UIColor clearColor];
[self.scrollView addSubview:tableView];
[tableView.leftAnchor constraintEqualToAnchor:self.scrollView.leftAnchor constant:8].active = YES;
[tableView.rightAnchor constraintEqualToAnchor:self.scrollView.rightAnchor constant:8].active = YES;
[tableView.topAnchor constraintEqualToAnchor:self.webView.topAnchor constant:8].active = YES;
My delegate methods :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(commentsArray!=nil){
return [commentsArray count];
}else
{
return 0;
}
}
And I am using custom table cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identity = #"CommentTableViewCell";
CommentTableViewCell *cell = (CommentTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identity];
if(cell == nil){
#try {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CommentTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
#catch (NSException *exception) {
NSLog(#"Err:%#", exception.reason);
}
#finally {
}
}
in your header file .h add the next code:
<UITableViewDelegate,UITableViewDataSource>
For example in UIVIewController Class
#interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
and this is all for you header file now got to .m and implement de nextlines:
this is for height:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{return 40.0f}
and after for this you add in your scrollview
scrollview.addSubview(yourTableView)
didLoad, now UILabel *lblPrice = [[UILabel alloc] init]; working but _webView = [[UIWebView alloc] init]; not working.
- (void)viewDidLoad {
[super viewDidLoad];
self.leftLabel = 0;
self.topLabel = 0;
self.statusWeb = 0;
[self.scrollView setBackgroundColor:[UIColor redColor]];
[lblFavoritesComment setText:[NSString stringWithFormat:#"%ld",(long)[_productModel totalFavorite]]];
[lblCommentCount setText:[NSString stringWithFormat:#"%ld",(long)[_productModel totalComments]]];
[lblProductName setText:[_productModel ProductName]];
lblDescription.lineBreakMode = UILineBreakModeWordWrap;
lblDescription.numberOfLines = 0;
[lblDescription setText:[_productModel Description]];
[lblDescription sizeToFit];
if([_productModel serviceHour] == 0){
NSString *serviceMinStr = [NSString stringWithFormat:#"%ld dk.",(long)[_productModel serviceMin]];
[lblTime setText:serviceMinStr];
}
if([_productModel serviceHour ] == 0 && [_productModel serviceMin] == 0){
[lblTime setText:#""];
}
if(![_productModel.Recipe isKindOfClass:[NSNull class]]){
if(![_productModel.Recipe isEqualToString:#"NULL"] || _productModel.Recipe.length>0){
self->statusWeb = 1;
_webView = [[UIWebView alloc] init];
_webView.translatesAutoresizingMaskIntoConstraints = false;
_webView.frame = CGRectMake(0,0,200, 300);
NSMutableString *html = [NSMutableString stringWithString: #"<html><head><title></title></head><body style=\"background:transparent;\">"];
[html appendString:[_productModel Recipe]];
[html appendString:#"</body></html>"];
[_webView setBackgroundColor:[UIColor blueColor]];
[_webView loadHTMLString:[html description] baseURL:nil];
[self.viewWebView addSubview:_webView];
[_webView.topAnchor constraintEqualToAnchor:self.viewShareBasketExtra.topAnchor constant:10].active = YES;
[_webView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor constant:8].active = YES;
[_webView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor constant:8].active = YES;
}
}else{
}
int multiPriceStatus = (int)[_productModel multiPrice];
if(multiPriceStatus == 1){
[self getMultiPrice];
}else{
UILabel *lblPrice = [[UILabel alloc] init];
[lblPrice setFont:[UIFont fontWithName:#"HelveticaNeue" size:14]];
lblPrice.translatesAutoresizingMaskIntoConstraints = false;
[lblPrice setTextAlignment:NSTextAlignmentCenter];
lblPrice.text = [NSString stringWithFormat:#"%.2f TL",[_productModel Price]];
[self.priceView addSubview:lblPrice];
[lblPrice.leftAnchor constraintEqualToAnchor:self.priceView.leftAnchor constant:8].active = YES;
[lblPrice.rightAnchor constraintEqualToAnchor:self.priceView.rightAnchor constant:8].active = YES;
[lblPrice.centerXAnchor constraintEqualToAnchor:self.priceView.centerXAnchor].active = YES;
[lblPrice.centerYAnchor constraintEqualToAnchor:self.priceView.centerYAnchor].active = YES;
[self getComments];
}
NSString *photo = [_productModel ProductImage];
photo = [photo stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding];
[imgProduct setShowActivityIndicatorView:YES];
[imgProduct setIndicatorStyle:UIActivityIndicatorViewStyleGray];
[imgProduct sd_setImageWithURL:[NSURL URLWithString:photo]
placeholderImage:[UIImage imageNamed:#"placeholder"] options:/* DISABLES CODE */ (0) == 0 ? SDWebImageRefreshCached : 0];
[imgProduct setContentMode:UIViewContentModeScaleAspectFit];
QewerFavoriteGesture *favoriteGesture = [[QewerFavoriteGesture alloc] initWithTarget:self action:#selector(addFavorite:)];
favoriteGesture.productId = [_productModel Id];
favoriteGesture.numberOfTapsRequired = 1;
[imgFavorite setUserInteractionEnabled:YES];
[imgFavorite addGestureRecognizer:favoriteGesture];
QewerFavoriteGesture *basketGesture = [[QewerFavoriteGesture alloc] initWithTarget:self action:#selector(showBasketWindow:)];
basketGesture.productId = [_productModel Id];
basketGesture.numberOfTapsRequired = 1;
[imgBasket setUserInteractionEnabled:YES];
[imgBasket addGestureRecognizer:basketGesture];
QewerFavoriteGesture *extraGesture = [[QewerFavoriteGesture alloc] initWithTarget:self action:#selector(openExtras:)];
extraGesture.productId = [_productModel Id];
extraGesture.numberOfTapsRequired = 1;
[imgExtra setUserInteractionEnabled:YES];
[imgExtra addGestureRecognizer:extraGesture];
QewerFavoriteGesture *commentGesture = [[QewerFavoriteGesture alloc] initWithTarget:self action:#selector(openComments:)];
commentGesture.productId = [_productModel Id];
commentGesture.numberOfTapsRequired = 1;
[lblCommentCount setUserInteractionEnabled:YES];
[lblCommentCount addGestureRecognizer:commentGesture];
}

Irregular space between the collectionview cells

I am new to iOS development and this is my first project that i am doing alone. i am trying to show the feed of my college's unofficial fb page using uicollectionview. I have been trying a lot of different things but nothing seems to work.
UICollectionViewController.m file
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"DCE Speaks Up";
self.collectionView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = NO;
NSError *error= nil;
NSURL *feedURL = [NSURL URLWithString:#"https://graph.facebook.com/382057938566656/feed?fields=id,full_picture,message,story,created_time,link&access_token=1750413825187852%7CkUl9nlZFPvdGxGZX4WYabKKG2G4"];
NSData *jsonData = [NSData dataWithContentsOfURL:feedURL];
NSDictionary *dataDictionary= [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
self.feedArray = [NSMutableArray array];
NSArray *feedTempArray = [dataDictionary objectForKey:#"data"]; //feedTempArray just used for parsing
for (NSDictionary *feedDictionary in feedTempArray) {
FacebookFeed *fbFeed =[FacebookFeed facebookFeedWithMessage:[feedDictionary objectForKey:#"message"]];
fbFeed.story = [feedDictionary objectForKey:#"story"];
fbFeed.imageURL = [NSURL URLWithString:[feedDictionary objectForKey:#"full_picture"]];
fbFeed.date = [feedDictionary objectForKey:#"created_time"];
fbFeed.sharedLink = [NSURL URLWithString:[feedDictionary objectForKey:#"link"]];
[self.feedArray addObject:fbFeed];
}
// Register cell classes
[self.collectionView registerClass:[FacebookCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
// Do any additional setup after loading the view.
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
FacebookCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
cell.feed = self.feedArray[indexPath.row];
cell.sizeDictionary = [self sizeForLabelAtIndexPath:indexPath.row collectionView:collectionView];
// Configure the cell
for (UIView *view in cell.contentView.subviews) {
if ([view isKindOfClass:[UILabel class]]) {
[view removeFromSuperview];
}else if ([view isKindOfClass:[UIImageView class]]){
[view removeFromSuperview];
}
}
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
return cell;
}
#pragma mark <UICollectionViewDelegateFlowLayout>
-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *sizeDictionary = [[NSDictionary alloc] init];
sizeDictionary = [self sizeForLabelAtIndexPath:indexPath.row collectionView:collectionView];
return CGSizeMake(self.view.frame.size.width - 20, [sizeDictionary[#"imageHeight"] floatValue] + [sizeDictionary[#"messageHeight"] floatValue] + [sizeDictionary[#"nameHeight"] floatValue]);
}
#pragma mark <UICollectionViewDelegate>
- (NSDictionary *) sizeForLabelAtIndexPath:(NSUInteger)indexPath collectionView:(UICollectionView *)collectionView{
FacebookFeed *feed = self.feedArray[indexPath];
CGSize nameSize = CGSizeFromString(#"DCE Speaks Up");
CGSize imageSize = CGSizeMake(0, 0);
if (feed.imageURL) {
imageSize = CGSizeMake(470, 394);
}
float height = 80;
NSString *string = #"";
if (feed.message) {
string = [feed.message stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}else if (feed.story){
string = [feed.story stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
if (string) {
NSDictionary *attributes = #{
NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleBody]
};
CGRect bodyFrame =
[string boundingRectWithSize:CGSizeMake(CGRectGetWidth(collectionView.bounds),
CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin)
attributes:attributes
context:nil];
height += ceilf(CGRectGetHeight(bodyFrame));
}
return #{ #"imageHeight" : #(imageSize.height) , #"messageHeight" : #(height) , #"nameHeight" : #(nameSize.height)};
}
Custom cell file
#implementation FacebookCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self)
{
self.contentView.backgroundColor = [UIColor whiteColor];
// change to our custom selected background view
self.imageView = [[UIImageView alloc] init];
self.messageLabel = [[UILabel alloc] init];
self.nameLabel = [[UILabel alloc] init];
}
return self;
}
-(void) setFeed:(FacebookFeed *)feed{
_feed = feed;
self.messageLabel.numberOfLines = 0;
self.messageLabel.lineBreakMode = 0;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data= [NSData dataWithContentsOfURL:feed.imageURL];
UIImage *theImage=[UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image= theImage ;
});
});
if (feed.message) {
self.messageLabel.text = [[NSString alloc] initWithString:[feed.message stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}else{
self.messageLabel.text = [[NSString alloc] initWithString:[feed.story stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
self.messageLabel.font = [UIFont fontWithName:#"ArialMT" size:11];
[self.messageLabel sizeToFit];
self.nameLabel.text = #"DCE Speaks Up";
[self.nameLabel setFont:[UIFont fontWithName:#"ArialMT" size:12]];
[self.nameLabel sizeToFit];
}
-(void) layoutSubviews{
NSLog(#"%#",self.feed);
self.nameLabel.frame = CGRectMake(10, 10, self.contentView.bounds.size.width, [self.sizeDictionary[#"nameHeight"] floatValue]);
float height = 200;
if (self.contentView.bounds.size.height - [self.sizeDictionary[#"messageHeight"] floatValue] -[self.sizeDictionary[#"nameHeight"] floatValue] >100) {
height = self.contentView.bounds.size.height - [self.sizeDictionary[#"messageHeight"] floatValue] -[self.sizeDictionary[#"nameHeight"] floatValue];
}
if (self.feed.imageURL) {
self.imageView.frame = CGRectMake(10, [self.sizeDictionary[#"messageHeight"] floatValue] +[self.sizeDictionary[#"nameHeight"] floatValue], self.contentView.bounds.size.width - 20,height);
self.messageLabel.frame = CGRectMake(10, [self.sizeDictionary[#"nameHeight"] floatValue] +10, self.contentView.bounds.size.width - 10, [self.sizeDictionary[#"messageHeight"] floatValue]);
[self.contentView addSubview:self.messageLabel];
[self.contentView addSubview:self.imageView];
}else{
self.messageLabel.frame = CGRectMake(10, [self.sizeDictionary[#"nameHeight"] floatValue], self.contentView.bounds.size.width - 10, [self.sizeDictionary[#"messageHeight"] floatValue]);
[self.contentView addSubview:self.messageLabel];
}
[self.contentView addSubview:self.nameLabel];
}
#end
Use NSLayoutConstraints, it's the standard and best practice. Also you shouldn't be adding subviews in layoutSubviews(), as this method may get called multiple times. I'd also suggest moving your cell size calculation code into your UICollectionViewCell subclass.
There is a lot of odd stuff going on in your code, so it's hard to tell what's causing your problems. It's probably a combination of all of the subview adding/removing and manual size calculations. You could also use the visual debugger to give you some clues as to what might be happening:
https://www.raywenderlich.com/98356/view-debugging-in-xcode-6
I believe your problem is here:
-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *sizeDictionary = [[NSDictionary alloc] init];
sizeDictionary = [self sizeForLabelAtIndexPath:indexPath.row collectionView:collectionView];
return CGSizeMake(self.view.frame.size.width - 20, [sizeDictionary[#"imageHeight"] floatValue] + [sizeDictionary[#"messageHeight"] floatValue] + [sizeDictionary[#"nameHeight"] floatValue]);
}
You set in CGSizeMake width:
self.view.frame.size.width - 20
And height:
[sizeDictionary[#"imageHeight"] floatValue] + [sizeDictionary[#"messageHeight"] floatValue] + [sizeDictionary[#"nameHeight"] floatValue]
Check it out if the size of your height it's correct. It looks like is too big. That method return the size of the each cell. I believe you have been setting a bigger height than you want to.

UiCollectionView freezes starting at a given index

According to some answers I found in Stackoverflow, I should use this piece of code:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self.collectionView scrollToItemAtIndexPath:selectedRow atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}
but the scrolling freezes, so I have to set it up this way:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.title = [NSString stringWithString:barTitle];
self.automaticallyAdjustsScrollViewInsets = NO;
//Download JSON
NSError *error=nil;
NSURL *url = [NSURL URLWithString:urlToFollow];
data = [NSMutableData dataWithContentsOfURL: url options:NSDataReadingMappedIfSafe error:&error];
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSArray *response = [dictionary objectForKey:#"items"];
articlesArray = [[NSArray alloc] initWithArray:response];
//sizes
CGFloat window_height = ([self window_height]-70.0);
CGFloat window_width = [self window_width];
CGRect frame = CGRectMake((window_width/2)-15.0f , window_height, 30.0f , 15.0f);
self.pageControl = [[UIPageControl alloc]initWithFrame:frame];
UICollectionViewFlowLayout *layout = (id) self.collectionView.collectionViewLayout;
layout.itemSize = self.collectionView.frame.size;
// Add a target that will be invoked when the page control is changed by tapping on it
[self.pageControl
addTarget:self
action:#selector(pageControlChanged:)
forControlEvents:UIControlEventValueChanged
];
// Set the number of pages to the number of pages in the paged interface
// and let the height flex so that it sits nicely in its frame
if (articlesArray.count >8) {
self.pageControl.numberOfPages = 10;
}else if (articlesArray.count <8){
self.pageControl.numberOfPages =[articlesArray count];
}
self.pageControl.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.pageControl];
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.collectionView scrollToItemAtIndexPath:selectedRow atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}
#pragma mark uicollectionview
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [articlesArray count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
LOArcticlesCustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"MyCell" forIndexPath:indexPath];
[cell.imageView setImageWithURL:[NSURL URLWithString:[[articlesArray objectAtIndex:indexPath.row]objectForKey:#"photoUrl"]]placeholderImage:[UIImage imageNamed:#"simboloLakari29.png"]];
index= indexPath.row;
cell.lblMake.text = [NSString stringWithString:[[articlesArray objectAtIndex:indexPath.row]objectForKey:#"marca"]];
cell.lblMake.lineBreakMode = NSLineBreakByWordWrapping;
cell.lblModel.text = [NSString stringWithString:[[articlesArray objectAtIndex:indexPath.row]objectForKey:#"modelo"]];
cell.lblModel.lineBreakMode = NSLineBreakByWordWrapping;
cell.lblPrice.text = [NSString stringWithString:[[articlesArray objectAtIndex:indexPath.row]objectForKey:#"precio"]];
cell.lblOrder.text = [NSString stringWithFormat:#"%ld de %ld", (unsigned long)indexPath.row+1, (unsigned long)articlesArray.count];
return cell;
}
#pragma mark page control
- (void)pageControlChanged:(id)sender
{
UIPageControl *pageControl = sender;
CGFloat pageWidth = self.collectionView.frame.size.width;
CGPoint scrollTo = CGPointMake(pageWidth * pageControl.currentPage, 0);
[self.collectionView setContentOffset:scrollTo animated:YES];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat pageWidth = self.collectionView.frame.size.width;
self.pageControl.currentPage = self.collectionView.contentOffset.x / pageWidth;
}
- (CGFloat) window_height {
return [UIScreen mainScreen].applicationFrame.size.height;
}
- (CGFloat) window_width {
return [UIScreen mainScreen].applicationFrame.size.width;
}
But the CollectionView goes from index=0 to the desired index and it's very annoying.
Any suggestion?

UIActivityIndicatorView Not Appearing

I am trying to show a UIActivityIndicatorView while my table view is loading data and have it disappear once loading is finished. The loading indicator never appears. What am I doing wrong?
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define storeURL [NSURL URLWithString: #"https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=25&playlistId=PL9DC706DCCCE00188&key=AIzaSyBS4do208_KPGHAhszfVkHadSvtfSgr7Mo"]
#import "BBYoutubeVideosTableViewController.h"
#import "Reachability.h"
#import "TSMessage.h"
#import "TSMessageView.h"
#import "YoutubeCell.h"
#import "KFBYoutubeVideoView.h"
#import "KFBAppDelegate.h"
#interface BBYoutubeVideosTableViewController ()
{
UIActivityIndicatorView *loadingIndicator;
}
#end
#implementation BBYoutubeVideosTableViewController
#synthesize title, videoID, thumbURL, descriptionString, url, titleArray, videoIDArray, thumbArray, descriptionArray;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil];
UIImageView *backgroundImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"plain_app-background.png"]];
CGFloat width = [[UIScreen mainScreen]bounds].size.width;
CGFloat height = [[UIScreen mainScreen]bounds].size.height;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
loadingIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(width / 2, height / 2, 37, 37)];
loadingIndicator.center = CGPointMake(width / 2, height / 2 - 37);
}
else
{
loadingIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(142, 365, 37, 37)];
}
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
loadingIndicator.hidesWhenStopped = YES;
Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
if(networkStatus == NotReachable)
{
[TSMessage showNotificationWithTitle:#"Network Error" subtitle:#"No active network connection!" type:TSMessageNotificationTypeError];
[loadingIndicator stopAnimating];
}
else {
[self.tableView addSubview:loadingIndicator];
[loadingIndicator startAnimating];
}
self.title = #"Bluegrass & Backroads";
self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
self.tableView.backgroundView = backgroundImage;
url = [NSURL URLWithString:#"https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=25&playlistId=PL9DC706DCCCE00188&key=AIzaSyBS4do208_KPGHAhszfVkHadSvtfSgr7Mo"];
dispatch_async(kBgQueue, ^{
NSData *data = [NSData dataWithContentsOfURL:url];
if (data == nil)
{
NSLog(#"data is nil");
}
else
{
[self performSelectorOnMainThread:#selector(fetchedData:) withObject:data waitUntilDone:YES];
}
});
}
- (void)viewDidDisappear:(BOOL)animated
{
[loadingIndicator stopAnimating];
}
- (void)fetchedData:(NSData *)responseData
{
NSError *error;
titleArray = [[NSMutableArray alloc]init];
videoIDArray = [[NSMutableArray alloc]init];
thumbArray = [[NSMutableArray alloc]init];
descriptionArray = [[NSMutableArray alloc]init];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSArray *items = [json objectForKey:#"items"];
for (NSDictionary *item in items)
{
NSDictionary *snippet = [item objectForKey:#"snippet"];
title = [snippet objectForKey:#"title"];
videoID = [[snippet objectForKey:#"resourceId"] objectForKey:#"videoId"];
thumbURL = [[[snippet objectForKey:#"thumbnails"] objectForKey:#"default"] objectForKey:#"url"];
descriptionString = [snippet objectForKey:#"description"];
[titleArray addObject:title];
[videoIDArray addObject:videoID];
UIImage *thumbnailImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:thumbURL]]];
[thumbArray addObject:thumbnailImage];
[descriptionArray addObject:descriptionString];
}
[self.tableView reloadData];
[loadingIndicator stopAnimating];
}
- (IBAction)morePressed:(id)sender
{
NSURL *kyfbVideos = [NSURL URLWithString:#"https://www.youtube.com/playlist?list=PL9DC706DCCCE00188"];
[[UIApplication sharedApplication] openURL:kyfbVideos];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [titleArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 215;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 60;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIColor *kfbBlue = [UIColor colorWithRed:8.0/255.0f green:77.0/255.0f blue:139.0/255.0f alpha:1];
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height)];
footerView.backgroundColor = [UIColor clearColor];
CGFloat width = footerView.frame.size.width;
UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
moreButton.backgroundColor = [UIColor clearColor];
[moreButton setTitle:#"More" forState:UIControlStateNormal];
[moreButton setTitleColor:kfbBlue forState:UIControlStateNormal];
moreButton.titleLabel.textAlignment = NSTextAlignmentCenter;
moreButton.titleLabel.font = [UIFont fontWithName:#"FranklinGothicStd-ExtraCond" size:25.0];
moreButton.frame = CGRectMake(width / 2 - 25, 0, 50, 50);
moreButton.layer.cornerRadius = 25.0;
moreButton.layer.borderWidth = 2.0f;
moreButton.layer.borderColor = kfbBlue.CGColor;
moreButton.clipsToBounds = YES;
moreButton.backgroundColor = [UIColor clearColor];
[moreButton addTarget:self action:#selector(morePressed:) forControlEvents:UIControlEventTouchUpInside];
[footerView addSubview:moreButton];
return footerView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIColor *kfbBlue = [UIColor colorWithRed:8.0/255.0f green:77.0/255.0f blue:139.0/255.0f alpha:1];
YoutubeCell *cell = [tableView dequeueReusableCellWithIdentifier:#"youtubeCell"];
if (!cell)
{
NSArray *nibs =[[NSBundle mainBundle] loadNibNamed:#"YoutubeCell" owner:self options:NULL];
cell = [nibs firstObject];
}
cell.videoTitle.text = [titleArray objectAtIndex:indexPath.row];
cell.videoDescription.text = [descriptionArray objectAtIndex:indexPath.row];
cell.videoThumbnail.image = [thumbArray objectAtIndex:indexPath.row];
cell.videoTitle.textColor = kfbBlue;
cell.videoDescription.textColor = kfbBlue;
cell.videoTitle.font = [UIFont fontWithName:#"FranklinGothicStd-ExtraCond" size:22.0];
cell.videoDescription.font = [UIFont fontWithName:#"FranklinGothicStd-ExtraCond" size:16.0];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
KFBYoutubeVideoView *videoView = [[KFBYoutubeVideoView alloc]init];
videoView.videoIDString = [videoIDArray objectAtIndex:indexPath.row];
videoView.videoTitle = [titleArray objectAtIndex:indexPath.row];
videoView.videoDescription = [descriptionArray objectAtIndex:indexPath.row];
[self.navigationController pushViewController:videoView animated:YES];
}
else
{
KFBYoutubeVideoView *videoView = [[KFBYoutubeVideoView alloc]initWithNibName:nil bundle:nil];
videoView.videoIDString = [videoIDArray objectAtIndex:indexPath.row];
videoView.videoTitle = [titleArray objectAtIndex:indexPath.row];
videoView.videoDescription = [descriptionArray objectAtIndex:indexPath.row];
NSMutableArray *details = [self.splitViewController.viewControllers mutableCopy];
UINavigationController *detailNav = [[UINavigationController alloc]initWithRootViewController:videoView];
[details replaceObjectAtIndex:1 withObject:detailNav];
KFBAppDelegate *appDelegate = (KFBAppDelegate *)[[UIApplication sharedApplication]delegate];
appDelegate.splitViewController.viewControllers = details;
appDelegate.window.rootViewController = self.splitViewController;
appDelegate.splitViewController.delegate = videoView;
[appDelegate.splitViewController viewWillAppear:YES];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I figured it out. tableView was being initialized after adding loadingIndicator as a subview.

Unable to use searchbar to filter tableview

I am trying to filter tableview with search bar. I have written all necessary methods.
My code is as below:
What is wrong with it?? Please help me, i could not find the solution.
There should be something that I am missing
#implementation ApothekeViewController
#synthesize tableView,searchBar,mapView,eczaneler,eczanelerTemp,locationManager,searchDispController,filteredListContent,isFiltered,tableViewController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
tableViewController = [[UITableViewController alloc] init];
tableViewController.tableView.delegate = self;
tableViewController.tableView.dataSource = self;
[tableViewController.tableView reloadData];
tableViewController.tableView.frame = CGRectMake(0,66 ,320.0, 768.0);
// tableView = [[UITableView alloc]init];
//tableView.frame = CGRectMake(0,66 ,320.0, 768.0);
//tableView.delegate = self;
//tableView.dataSource = self;
[self.view addSubview:tableViewController.tableView];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.placeholder = #"Arama" ;
searchBar.delegate = self;
UISearchDisplayController *searchCon = [[UISearchDisplayController alloc]
initWithSearchBar:searchBar
contentsController:tableViewController];
self.searchDispController = searchCon;
searchDispController.delegate = self;
searchDispController.searchResultsDataSource = self;
searchDispController.searchResultsDelegate = self;
//searchBar.showsCancelButton = YES;
tableViewController.tableView.tableHeaderView = searchBar;
NSDate *now = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init ] ;
[formatter setDateFormat:#"dd MMMM yyyy, EEEE"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"Australia/Sydney"]];
NSString *strSelectedDate= [formatter stringFromDate:now];
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 55)];
UILabel *date = [[UILabel alloc] initWithFrame:CGRectMake(760,25,400,30)];
date.text = strSelectedDate;
date.textColor = [UIColor whiteColor];
[date setFont:[UIFont fontWithName:#"Arial" size:25]];
[naviBarObj addSubview:date];
[date setBackgroundColor:[UIColor clearColor]];
naviBarObj.topItem.titleView = date;
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(60,5,400,50)];
title.text = #"En Yakın Eczane";
title.textColor = [UIColor whiteColor];
[title setFont:[UIFont fontWithName:#"Arial" size:40]];
[naviBarObj addSubview:title];
[title setBackgroundColor:[UIColor clearColor]];
naviBarObj.topItem.titleView = title;
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc]
initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(cancel_Clicked:)];
naviBarObj.topItem.leftBarButtonItem = btnCancel;
[self.view addSubview:naviBarObj];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(320, 66, 704, 705)];
mapView.delegate = self;
[self.view addSubview:mapView];
mapView.showsUserLocation = YES;
eczanelerTemp = [[NSMutableArray alloc]init];
self.eczaneler = [[NSMutableArray alloc] init];
Eczane *eczane1 = [[Eczane alloc] init];
eczane1.customerName = #"Duyarlı Eczanesi";
eczane1.customerID = #"mohammed#gmail.com";
eczane1.longitude = #"29.0312";
eczane1.latitude = #"41.109817";
Eczane *eczane2 = [[Eczane alloc] init];
eczane2.customerName = #"Azim Eczanesi Azim";
eczane2.customerID = #"azim#gmail.com";
eczane2.longitude = #"30.0312";
eczane2.latitude = #"41.109817";
[self.eczaneler addObject:eczane1];
[self.eczaneler addObject:eczane2];
Eczane *eczane3 = [[Eczane alloc] init];
eczane3.customerName = #"Optik Eczanesi";
eczane3.customerID = #"optik#gmail.com";
eczane3.longitude = #"29.0352";
eczane3.latitude = #"40.109817";
[self.eczaneler addObject:eczane3];
for(int i=0; i<[eczaneler count];i++)
{
Eczane *temp = [self.eczaneler objectAtIndex:i];
CLLocationCoordinate2D coordinate;
coordinate.longitude = [temp.longitude floatValue];
coordinate.latitude = [temp.latitude floatValue];
CustomAnnotation *point = [[CustomAnnotation alloc] init];
point.coordinate = coordinate;
[self.mapView addAnnotation:point];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.mapView.delegate = self;
[self.mapView setShowsUserLocation:YES];
return YES;
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView2 viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
else if ([annotation isKindOfClass:[CustomAnnotation class]])
{
static NSString * const identifier = #"MyCustomAnnotation";
MKAnnotationView* annotationView = [mapView2 dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView)
{
annotationView.annotation = annotation;
}
else
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:identifier];
}
// set annotationView properties
UIImageView * ecz = [[UIImageView alloc]init];
ecz.frame = CGRectMake(0, 0, 65, 49);
ecz.image = [UIImage imageNamed:#"indicator.png"];
UIImageView * vstd = [[UIImageView alloc]init];
vstd.frame = CGRectMake(33, 10, 24, 22);
vstd.image = [UIImage imageNamed:#"indicator_ziyaret_gri"];
UIImageView * nbox = [[UIImageView alloc]init];
nbox.frame = CGRectMake(48, -6, 22, 22);
nbox.image = [UIImage imageNamed:#"numara_kutusu"];
[annotationView addSubview:ecz];
[annotationView addSubview:vstd];
UILabel *index = [[UILabel alloc] initWithFrame:CGRectMake(5,4,15,15)];
// index.text =[NSString stringWithFormat:#"%d", iterator ];
index.textColor = [UIColor whiteColor];
[index setFont:[UIFont fontWithName:#"Arial-BoldMT" size:18]];
[nbox addSubview:index];
[index setBackgroundColor:[UIColor clearColor]];
[annotationView addSubview:nbox];
//annotationView. = [UIImage imageNamed:#"indicator.png"];
annotationView.canShowCallout = YES;
return annotationView;
}
return nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.tableViewController.tableView == self.searchDisplayController.searchResultsTableView) {
return [filteredListContent count];
} else {
return [eczaneler count];
}
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 110;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"cellForRowAtIndexPath");
static NSString *CellIdentifier = #"EczaneCell";
EczaneCell *cell = (EczaneCell *) [self.tableViewController.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[EczaneCell alloc] initWithFrame:CGRectZero] autorelease];
}
Eczane *eczane = [[Eczane alloc]init];
if (self.tableViewController.tableView == self.searchDisplayController.searchResultsTableView) {
eczane = [self.filteredListContent objectAtIndex:[indexPath row]];
} else {
eczane = [self.eczaneler objectAtIndex:[indexPath row]];
}
cell.circleIndex.text = [NSString stringWithFormat:#"%d", ([indexPath row]+1)];
cell.cellName.text = eczane.customerName;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//EczaneCell *cell = [tableView cellForRowAtIndexPath:indexPath];
Eczane *eczane = [self.eczaneler objectAtIndex:[indexPath row]];
CLLocationCoordinate2D coordinate;
coordinate.longitude = [eczane.longitude floatValue];
coordinate.latitude = [eczane.latitude floatValue];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000);
[mapView setRegion:region animated:YES];
}
#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[filteredListContent removeAllObjects];
for (Eczane *eczObj in eczaneler)
{
NSComparisonResult result = [eczObj.customerName compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])];
if (result == NSOrderedSame)
{
[self.filteredListContent addObject:eczObj];
}
}
[self.searchDispController.searchResultsTableView reloadData];
//[self.tableViewController.tableView reloadData];
//[self.tableViewController.tableView] = [self.searchDispController.searchResultsTableView ];
[self.tableViewController.tableView reloadData];
[searchDispController.searchResultsTableView setFrame:CGRectMake(0, 44, 320, [filteredListContent count]*(searchDispController.searchResultsTableView.rowHeight))];
return YES;
}
#end
Your problem is probably:
contentsController:tableViewController
Because you have 'stolen' the table view from the controller and made yourself the delegate and data source so the search controller will be trying to gather source information using a broken controller.
Make self the contents controller.
Your next problem is:
if (self.tableViewController.tableView == self.searchDisplayController.searchResultsTableView) {
Again, because you broke that controller (you should fix that, why are you creating a table view controller and then stealing its view?). Change it to:
if (tableView == self.searchDisplayController.searchResultsTableView) {

Resources