How can we add button in custom cell? - iphone

I am working in a app and need to add button in cell by customcell.
Can any one help me for this?
Thanks in advance

You add a button in Customcell so use the following code.
First of all , you add new file with subclass of UITableViewCell and then
Customcell.h file
#interface CustomCell : UITableViewCell
{
UIButton *customButton;
}
#property (nonatomic , retain) UIButton *customButton;
Customcell.m
#implementation CustomCell;
#synthesize customButton;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]))
{
customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setImage:[UIImage imageNamed:#"phonecall.png"] forState:UIControlStateNormal];
}
return self;
}
// set layout in subview
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGFloat boundY = contentRect.origin.y;
CGRect frame1;
frame1 = CGRectMake(boundsX+10, boundY+58, 19, 18);
customButton.frame = frame1;
}
#import "CustomCell.h"
In cellForRowAtIndexPath method
// Customize the number of cells in the table view.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell= [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
if (cell == nil)
{
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
[cell.customButton addTarget:self action:#selector(callAction:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}

Related

Setting Position of the textlabel inside the tableviewcell?

In my application i am trying to adjust the position of the textlabel and detailtextlabel in a tableview cell.I am doing like this `
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"cell_with_arrow.png"]];
[cell setBackgroundView:img];
[img release];
}
cell.textLabel.frame = CGRectMake(0,0, 150, 40);
cell.detailTextLabel.frame = CGRectMake(155, 55, 150, 40);
cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14.0];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.text =#"name";
cell.detailTextLabel.text=#" status";
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
//cell.detailTextLabel.textAlignment = UITextAlignmenteft;
//cell.textLabel.textAlignment = UITextAlignmentLeft;
return cell;
}
`but seems to be having no effect.can anybody help me to point me where i am going wrong?
textLabel and detailTextLabel are readonly so you can not change them.
#property(nonatomic, readonly, retain) UILabel *textLabel
#property(nonatomic, readonly, retain) UILabel *detailTextLabel
See here
What I would suggest is that you could create your custom UITableViewCell instead, you will have more control over it's content and can add custom subviews in there as well.
Here are some tutorials for that:
From IB
From Code
you need to override layoutSubviews method
Create a customcell class and inherit with uitableviewcell
#try this code
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell
#end
CustomCell.m
#import "CustomCell.h"
#implementation CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
-(void)layoutSubviews{
//Set here your frame
self.textLabel.frame = CGRectMake(0, , 320, 30);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end

get warning :UITableView dataSource must return a cell

I am having ViewAController:UITableViewController.I also have firstNameCell and lastNameCell like below
In ViewAController, I do
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
if (indexPath.section == 0)
return firstNameCell;
return lastNameCell;
}
My question : why my cells are nil even though they are connecting to the cell in xib
Please advice me on this issue.
Your cellForRowAtIndexPath method does not initialize the cell properly.
It should contain code this -
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
It's possible you are being called before the nib has finished loading. Check to see if your -awakeFromNib method has been called yet.
Please try this:
static NSString *CellIdentifier = #"Cell";
cellVideoOptions *cell =(cellVideoOptions *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
and then init cell with nib name.
Do it programmatically:
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell
{
UILabel *firstName;
UILabel *lastName;
}
#property (nonatomic, retain) UILabel *firstName;
#property (nonatomic, retain) UILabel *lastName;
#end
CustomCell.m
#import "CustomCell.h"
#implementation CustomCell
#synthesize firstName, lastName;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
firstName = [[UILabel alloc]init];
firstName.textAlignment = UITextAlignmentLeft;
firstName.font = [UIFont boldSystemFontOfSize:18];
firstName.backgroundColor = [UIColor clearColor];
lastName = [[UILabel alloc]init];
lastName.textAlignment = UITextAlignmentLeft;
lastName.font = [UIFont boldSystemFontOfSize:14];
lastName.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:firstName];
[self.contentView addSubview:lastName];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+7 ,7, 240, 20);
firstName.frame = frame;
frame= CGRectMake(boundsX+125 ,25, 220, 20);
lastName.frame = frame;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
- (void)dealloc
{
[firstName release];
[lastName release];
[super dealloc];
}
#end
In your ViewAController with TableView, dont forget import CustomCell.h
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
if (indexPath.section == 0) {
cell.firstName.text = #"firstname";
// and or cell.firstName.frame = ...
} else {
cell.lastName.text = #"lastname"
// and or cell.lastName.frame = ...
}
return cell;
}

can't change UITableViewCell background color

I have UITableViewCell that fits on UITableView, but somehow I can't change the background color when entering editing style mode.. I've tried everything on the web, search all day long, but still couldn't get it fixed (I've searched StackOverflow as-well). please, help me.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"MainTableViewCell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (MainTableViewCell *)view;
}
}
}
Try customizing the cell inside tableView: willDisplayCell: method, something like this:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];
}
You need to change the tableView cell's contentView, not the cell's background view.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.contentView.backgroundColor = [UIColor redColor];
}
to have total control on the customisation of your cells, I prefer better to override the cell view,
create a new class for your cell view, that gets called by the UITableView,
later when i get to my work computer if you havent found your answer I will post some sample code,
once you see how it works is pretty easy
you could as well place an image for your cell background, and place different labels and images, buttons, textfields in custom places of your cell,
EDIT>>
the code! [is overkill just to change the background, but if you want to really customise your cell, this is the way to go!]
in your CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell {
UILabel *_kLabel;
UILabel *_dLabel;
}
#property (nonatomic, retain) UILabel *kLabel;
#property (nonatomic, retain) UILabel *dLabel;
- (void) initLabels;
#end
in your CustomCell.m
#import "CustomCell.h"
#implementation CustomCell
#synthesize kLabel = _kLabel;
#synthesize dLabel = _dLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
CGRect popUpImageBgndRect = CGRectMake(0, 0, 942, 44);
UIImageView *popUpImageBgnd = [[UIImageView alloc] initWithFrame:popUpImageBgndRect];
[popUpImageBgnd setImage:[UIImage imageNamed:#"tableCellBgnd.png"]];
popUpImageBgnd.opaque = YES; // explicitly opaque for performance
[self.contentView addSubview:popUpImageBgnd];
[popUpImageBgnd release];
[self initLabels];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+10 ,10, 200, 20);
self.kLabel.frame = frame;
frame= CGRectMake(boundsX+98 ,10, 100, 20);
self.dLabel.frame = frame;
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void) initLabels {
self.kLabel = [[[UILabel alloc]init] autorelease];
self.kLabel.textAlignment = UITextAlignmentLeft;
self.kLabel.backgroundColor = [UIColor clearColor];
self.kLabel.font = [UIFont fontWithName:#"FS Albert" size:16];
self.kLabel.textColor = [UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1];
self.dLabel = [[[UILabel alloc]init] autorelease];
self.dLabel.textAlignment = UITextAlignmentLeft;
self.dLabel.backgroundColor = [UIColor clearColor];
self.dLabel.font = [UIFont systemFontOfSize:16];
self.dLabel.textColor = [UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1];
[self.contentView addSubview:self.kLabel];
[self.contentView addSubview:self.dLabel];
}
-(void) dealloc {
[_kLabel release];
[_dLabel release];
[super dealloc];
}
#end
And in your ViewController.m
YourViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}
ENJOY!!
;)
try setting the cells backgroundColor property, worked for me cell.backgroundColor=[UIColor blueColor];
Please try this, it works for me.
UIView *bgView = [[UIView alloc] initWithFrame:cell.frame];
bgView.backgroundColor = [UIColor greenColor];
cell.backgroundView = bgView;
[bgView release];
As #Westley mentioned;
You need to change the tableView cell's contentView, not the cell's background view.
This is how you should do it:
if(index == conditionYouWant)
{
cell.contentView.backgroundColor = [UIColor whiteColor];
}
else
{
cell.contentView.backgroundColor = [UIColor colorWithRed:0.925 green:0.933 blue:0.937 alpha:1.0];
}
Hope it helps you guys out
Be sure you didn't already set the content view's background color in the Storyboard. If you did, changing the cell.backgroundColor in code will make no difference, and you will go round and round in confusion (like I did).
cell.backgroundColor = [UIColor redColor];
in swift 3 you can use
cell.backgroundcolor = UIColor.white

How to make UITableView Like Below Image?

How to make UITableView like above image?
I know this is grouped table type. but how we can add the image+label+button to header of section.
I have tried
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
but it starts with CGRectMake(0,0,320,height).
I want just above section and exact width of section just like in image.
Thanks in advance.
Instead of trying to change the section header view, you might want to create a custom cell with a brown background, a label and a button and use it for the first row. So, in -cellForRowAtIndexPath, you could do something like
if (0 == indexPath.row) {
return brownCell;
} else {
return normalCell;
}
There are several ways to create custom cells, I always start from the Table View Programming Guide for iOS.
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell{
id delegate;
NSIndexPath *indexpath;
}
#property(nonatomic,assign) id delegate;
#property(nonatomic,retain)NSIndexPath *indexpath;
#property(nonatomic,retain) IBOutlet UIToolbar *Toolbar;
-(IBAction)SelectorLeft:(id)sender;
-(IBAction)SelectorRight:(id)sender;
#end
customcell.m
#import "CustomCell.h"
#import <QuartzCore/QuartzCore.h>
#implementation CustomCell
#synthesize Toolbar;
#synthesize delegate,indexpath;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
-(IBAction)SelectorLeft:(id)sender{
[delegate perfromselector:#selector(left:) withObject:indexpath];
}
-(IBAction)SelectorRight:(id)sender{
[delegate perfromselector:#selector(left:) withObject:indexpath];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
UItbaleView part
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"identifier";
NSUInteger row = [indexPath row];
if (row == 0) {
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
self.Cell = nil;
[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = self.Cell;
}
cell.Toolbar.clipsToBounds=YES;
CALayer *l=cell.Toolbar.layer;
// set corner radious
[l setCornerRadius:10];
// to apply border on corners
[l setBorderColor:[[UIColor clearColor] CGColor]];
// to apply set border width.
[l setBorderWidth:5.0];
return cell;
}else{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat: #"cell %i",row];
cell.delegate = self;
cell.indexpath = indexpath;
return cell;
}
return nil;
}
Also do n't forget to create Customcell.xib and add tool bar through interface builder
also create an outlet of CustomCell in tableview class and handle it as above
its easy all what you have to
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
if (row % 2 == 0) {
static NSString *identifier = #"RowOne";
UITableViewCell *cell = (RowTypeOne*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault]autorelease];
}
cell.Title.text = [datasource objectatindex:row];
cell.Title.font = [UIFont fontWithName:#"Tahoma" size:16];
cell.contentView.backgroundColor = [UIColor redColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textAlignment = UITextAlignmentRight;
return cell;
}else if (row % 2 == 1) {
static NSString *identifier = #"RowTwo";
UITableViewCell *cell = (RowTypeOne*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault]autorelease];
}
cell.contentView.backgroundColor = [UIColor redColor];
cell.Title.text = [datasource objectatindex:row];
cell.Title.font = [UIFont fontWithName:#"Tahoma" size:16];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textAlignment = UITextAlignmentRight;
return cell;
}
return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//create btn left each on handeled by selector
UIBarButtonItem *btnleft = [[UIBarButtonItem alloc] initWithTitle:#"List of sms" style:UIBarButtonItemStylePlain target:self action:#selector(ListClicked:)];
//create btn right each on handeled by selector
UIBarButtonItem *btnright = [[UIBarButtonItem alloc] initWithTitle:#"New" style:UIBarButtonItemStyleBordered target:self action:#selector(NewClicked:)];
//create uitoolbar then add btns to it as list of array
UIToolbar *tool = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
//change style of toolbar to be black
[tool setBarStyle:UIBarStyleBlack];
tool.items = [NSArray arrayWithObjects:btnleft,[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],btnright, nil];
//this is the parent view that we will add on it the uitoolbar objects and the buttons
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[view autorelease];
[view addSubview:tool];
return view;
}
-(void)ListClicked:(id)sender{
//handle here btn left
}
-(void)NewClicked:(id)sender{
//handle here btn right
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 44;
}
if you want the cell not to be in the header you can check for first row in cellforrow at index path...
also if you want to do it in another style you can make custom cell and add it also to cellforrowatindexpath

Change gradient of background color with respect to UISlider value placed into TableViewCell

i am new to objective C. I want to Change the gradient of background color of a Lable placed in the Cell
with respect to the UIslider placed in same cell.
As the slider moves from Left to Right the gradient of the Background color should change.
Can anyone guide me how to do it???
This code will change alpha of background color of UILabel as per slider value change.
first you need to declare slider in custom Cell. as describe bellowed.
Declare slider in SignUpCustomCell.h cell.
#import <UIKit/UIKit.h>
#interface SignUpCustomCell : UITableViewCell {
UISlider* slider;
UILabel *lblLeft;
}
#property(nonatomic,retain) UISlider* slider;
#property(nonatomic,retain) UILabel *lblLeft;
#end
alloc memory in SignUpCustomCell.m file.
#import "SignUpCustomCell.h"
#synthesize slider,lblLeft;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
slider=[[UISlider alloc]initWithFrame:CGRectMake(150, 5, 100, 25)];
[slider setValue:0.0];
slider.minimumValue=0;
slider.maximumValue=1;
[self.contentView addSubview:self.slider];
self.lblLeft = [[UILabel alloc]init];
self.lblLeft.font = [UIFont fontWithName:#"Helvetica-Bold" size:12];
self.lblLeft.textColor = [UIColor colorWithRed:135.0/255.0 green:135.0/255.0 blue:135.0/255.0 alpha:1.0];
self.lblLeft.backgroundColor = [UIColor greenColor];
[self.lblLeft setTextAlignment:UITextAlignmentLeft];
[self.contentView addSubview:self.lblLeft];
[self.lblLeft release];
}
return self;
}
After creating custom cell.You will used it where you want.
To used custome cell in SignupViewController.m . we need following steps.
#import "SignUpCustomCell.h"
Then write code in cellForRowAtIndexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
SignUpCustomCell *cell = (SignUpCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[SignUpCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.lblLeft.text=#"test app";
cell.slider.tag=indexPath.row;
[cell.slider addTarget:self action:#selector(sliedervalue:) forControlEvents:UIControlEventValueChanged];
return cell;
}
-(void)sliedervalue:(id)sender
{
UISlider* sl=(UISlider*)sender;
NSLog(#"sl=%d",sl.tag);
NSLog(#"sl_value=%f",sl.value);
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:sl.tag inSection:0] ;
SignUpCustomCell *cell = (SignUpCustomCell*)[tblSignup cellForRowAtIndexPath:indexPath];
cell.lblLeft.alpha= sl.value;
}
Let me know if you have any query.