Display a custom background picture in UISearchResultsTableView in iOS - uitableview

Currently I have this code that changes the default background in a TableView to a custom picture, but when the user does a search in the UISearchBar, the Search Results TableView is a plain white background. Where/how do I change it to be the same as my regular non-searched TableView?
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Employee List", #"Employee List");
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
}
else {
{
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Cloth.png"]];
[tempImageView setFrame:self.tableView.frame];
self.tableView.backgroundView = tempImageView;
}
}
}
return self;
}

You should be able to access the searchResults TableView through the searchDisplay Controller:
...
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Cloth.png"]];
[tempImageView setFrame:self.tableView.frame];
self.tableView.backgroundView = tempImageView;
self.searchDisplayController.searchResultsTableView.backgroundView = tempImageView;
...

Related

UI code works but change to XIB failed

the code below works
#interface MyUICollectionViewCell : UICollectionViewCell {
}
#implementation UICollectionViewCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self = [super initWithFrame:frame];
if (self) {
_moreView = [[UIView alloc] init];
_moreView.backgroundColor = [UIColor clearColor];
_moreView.frame = CGRectMake(self.frame.size.width - 70, 0, 45, 37);
[self addSubview:_moreView];
_moreView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapMoreView = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(goToDoSomething)];
[_moreView addGestureRecognizer:tapMoreView];
}
return self;
}
return self;
}
- (void)doSomeThing
{
}
when I touch the subview (gray block), it will trigger the event,
but I change to XIB(the sub view links to IBOutlet mUIView), and change the code as below, goToDoSomething cannot be triggered.
Your comment welcome
#interface MyUICollectionViewCell : UICollectionViewCell {
IBOutlet UIView *mUIView;
}
#implementation UICollectionViewCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self = [super initWithFrame:frame];
if (self) {
NSArray *nibView = [[NSBundle mainBundle] loadNibNamed:#"MyUICollectionViewCell"owner:self options:nil];
UIView *bw = [nibView objectAtIndex:0] ;
bw.userInteractionEnabled = YES;
[self.contentView addSubview:bw];
mUIView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapMoreView = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(goToDoSomething)];
[mUIView addGestureRecognizer:tapMoreView];
}
return self;
}
return self;
}
- (void)doSomeThing
{
}

Custom TableViewCell With Image Disappearing When Selected

I have created a custom tableviewcell class and the cells load perfectly, however, when I click on them they disappear and come back when I click on a different cell. Can anyone help me understand why? Thanks in advance!
#import "CustomTableViewCell.h"
#implementation CustomTableViewCell
UIView *backgroundView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
UIView *visibleBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(29, 0, backgroundView.bounds.size.width -58, backgroundView.bounds.size.height)];
[visibleBackgroundView setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"tableViewCell.png"]]];
[backgroundView addSubview:visibleBackgroundView];
self.backgroundView = backgroundView;
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
if (selected) {
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
UIView *visibleSelectedBackground = [[UIView alloc] initWithFrame:CGRectMake(29, 0, backgroundView.bounds.size.width -58, backgroundView.bounds.size.height)];
[visibleSelectedBackground setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"selectedTableViewCell#2x.png"]]];
[selectedBackgroundView addSubview:visibleSelectedBackground];
self.selectedBackgroundView = selectedBackgroundView;
}
}
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
[self setHighlighted:NO];
}
}
I was able to fix it with the following solution
#import "CustomTableViewCell.h"
#implementation CustomTableViewCell{
UIView *backgroundView;
UIView *visibleBackgroundView;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
visibleBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(29, 0, backgroundView.bounds.size.width -58, backgroundView.bounds.size.height)];
[visibleBackgroundView setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"tableViewCell.png"]]];
[backgroundView addSubview:visibleBackgroundView];
self.backgroundView = backgroundView;
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
if (selected) {
}
}
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
[visibleBackgroundView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"selectedTableViewCell.png"]]];
} else{
[visibleBackgroundView setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"tableViewCell.png"]]];
}
}

Adjust iOS TabBar item title to the centre of it

//TabBarController code:
self.delegate=self;
self.tabBarController.tabBar.delegate=self;
CGRect viewFrame=self.tabBar.frame;
viewFrame.origin.y -=0;![enter image description here][1]
viewFrame.origin.x -=0;
viewFrame.size.height=30;
self.tabBar.frame=viewFrame;
firstViewController = [[FirstViewController alloc] initWithNibName:nil bundle:NULL];
secondViewController = [[SecondViewController alloc] initWithNibName:nil bundle:NULL];
NSArray *twoViewControllers = [[NSArray alloc] initWithObjects:
self.firstViewController, self.secondViewController, nil];
self.viewControllers=twoViewControllers;
// ====================================================
//
// FirstViewController code in initWithNibName:
//
// To set the title of the first tabbar item:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Article view";
NSLog(#"count = %d",[self.tabBarController.tabBar.items count]);
}
return self;
}
//How can i make the first tabbar item title "Article View" to the Center without adding any //image to the tabbaritem ?
// similar to the below tabbar items screenshot.
[1]: http://i.stack.imgur.com/xBpVH.png
Thanks in advance.
Replace the initWithNibName method
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Article view";
self.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
NSLog(#"count = %d",[self.tabBarController.tabBar.items count]);
}
return self;
}
self.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0); this line here adjusts the position of the image for the tabBarItem in the manner :
Shift the image in x-direction '+5' and in y-direction '-5' from the
default position.
Play with UIEdgeInsetsMake and have fun. Cheers.

Add gestures to UISearchResults UITableView

Currently my app has gestures, if you hold down on a UITableView cell you edit the object in the cell, if you tap the cell you view the object in the cell. That part works just fine but when the user uses the UISearchBar the results table does not have the same gestures.
Should I be adding the same gestures into else section of the initWithNibName?
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"AutoNotes2", #"AutoNotes2");
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
}
else {
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Concrete.png"]];
[tempImageView setFrame:self.tableView.frame];
self.tableView.backgroundView = tempImageView;
self.searchDisplayController.searchResultsTableView.backgroundView = tempImageView;
}
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap)];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
// Two finger, double tap
UITapGestureRecognizer *twoFingerDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTwoFingerDoubleTap:)];
twoFingerDoubleTap.numberOfTapsRequired = 1;
twoFingerDoubleTap.numberOfTouchesRequired = 2;
[self.tableView addGestureRecognizer:twoFingerDoubleTap];
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLongPress:)];
lpgr.minimumPressDuration = 1.0; //seconds
//lpgr.delegate = self;
[self.tableView addGestureRecognizer:lpgr];
}

issue with using multiple cell types in uitableview

I am using this code for multiple cell types in a UITableView
The problem is that the cell text is invisible. The code for cellForRowAtIndexPath as well as the cell class code is given below:
code:
static NSString *kCellIdentifier = #"NewsViewControllerTableCell";
static NSString *kCellIdentifier2 = #"SubscribeCell";
if ((indexPath.row==0) && ([[NSUserDefaults standardUserDefaults] boolForKey:#"subscribeButtonOption"]))
{
SubscribeCell* cell = (SubscribeCell*)[tableView dequeueReusableCellWithIdentifier:kCellIdentifier2];
if (cell == nil) {
cell = [[[SubscribeCell alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 35.0) reuseIdentifier:kCellIdentifier2] autorelease];
cell.contentView.backgroundColor = kColorR53G53B53;
cell.subscribeLabel.font = kLucidaSansStdFontBold_14;
cell.subscribeLabel.textColor = [UIColor whiteColor];
}
cell.subscribeLabel.textColor=[UIColor redColor];
cell.subscribeLabel.text = #"+ SUBSCRIBE TO NEWSLETTER";
cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
cell.selectedBackgroundView.backgroundColor =kColorR53G53B53;
[cell setNeedsDisplay];
return cell;
}
else
{
//another cell
}
=========
header:
#import <UIKit/UIKit.h>
#interface SubscribeCell : UITableViewCell{
UILabel *subscribeLabel;
}
#property(nonatomic, retain) UILabel *subscribeLabel;
#end
and implementation class:
#import "SubscribeCell.h"
#implementation SubscribeCell
#synthesize subscribeLabel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
subscribeLabel=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 323.0, 40.0)];
subscribeLabel.textColor=[UIColor whiteColor];
self.backgroundColor=kColorR53G53B53;
}
return self;
}
Check to see if subscribeLabel is nil. You're creating it in initWithNibName:bundle: but are initializing with initWithFrame:reuseIdentifier:, so it's not reaching your label creation code.
If I try to compile your code, I get an error message stating that UITableViewCell does not declare a method called 'initWithNibName: bundle:'. You should use the proper initialization method 'initWithStyle: reuseIdentifier:'. You also forget to add the subscribeLabel to the contentView of the cell.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
subscribeLabel=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 323.0, 40.0)];
subscribeLabel.textColor=[UIColor whiteColor];
[self.contentView addSubview:subscribeLabel];
self.backgroundColor=kColorR53G53B53;
}
return self;
}

Resources