I have a table view in one view controller with five cells on clicking of which I redirect to some other view controller. The issue is that whenever I come back the view controller that contains the table view, the contents of the last cell in the table view automatically disappears.
Here is the code for my tableview:
-(void)viewWillAppear:(BOOL)animated
{
logoimg=[[NSMutableArray alloc]initWithObjects:#"account_settings.png", #"account_settings_onclick.png",#"invite_friends.png", #"invite_friends_onclick.png", #"leaderboard.png", #"leaderboard_onclick.png", #"setting.png",#"setting_onclick.png", #"logout.png", #"logout_onclick.png", nil];
logoname=[[NSMutableArray alloc]initWithObjects:#"Account Settings", #"Invite Friends",#"Leaderboard",#"Settings",#"Logout",nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 45;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return logoname.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"%d_%d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.backgroundColor = [UIColor clearColor];
//[[[cell contentView] subviews] makeObjectsPerformSelector: #selector(removeFromSuperview)];
UIImage *logoOff = [UIImage imageNamed:[logoimg objectAtIndex:indexPath.row*2]];
UIImage *logoOn = [UIImage imageNamed:[logoimg objectAtIndex:indexPath.row*2+1]];
UIButton *logo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[logo setBackgroundImage:logoOff forState:UIControlStateNormal];
[logo setBackgroundImage:logoOn forState:UIControlStateSelected];
[logo setBackgroundImage:logoOn forState:UIControlStateHighlighted];
logo.frame = CGRectMake(20, 5, logoOff.size.width/2, logoOff.size.height/2);
logo.userInteractionEnabled = NO;
logo.tag = indexPath.row;
logo.selected = FALSE;
//[bottomView addSubview:logo];
UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(70,13, cell.contentView.frame.size.width-70, 20)];
[name setFont:[UIFont fontWithName:#"CenturyGothic" size:14]];
name.numberOfLines = 2;
name.text=[logoname objectAtIndex:indexPath.row];
name.backgroundColor = [UIColor clearColor];
[name sizeToFit];
name.textColor = [UIColor whiteColor];
UIImageView *divider = [[UIImageView alloc]init];
divider.image = [UIImage imageNamed:#"divider_line.png"];
divider.frame = CGRectMake(0, 43, self.view.frame.size.width ,2);
[cell.contentView addSubview:logo];
[cell.contentView addSubview:name];
[cell.contentView addSubview:divider];
return cell;
}
The logoname and logoimage are two NSMutableArrays.
If any more code is needed let me know I will provide the necessary code.
Better u need to subclass the tableview because, each time you are reusing the cell, but its subview are not, so just do like this
create a new file and name it as MyCustomCell subclass of UITableViewCell
in MyCustomCell.h file
MyCustomCell.h
#import <UIKit/UIKit.h>
#interface MyCustomCell : UITableViewCell
#property (nonatomic, retain) UIImage *logoOff;
#property (nonatomic, retain) UIImage *logoOn;
#property (nonatomic, retain) UIButton *logo;
#property (nonatomic, retain) UILabel *name;
#property (nonatomic, retain) UIImageView *divider;
#end
in MyCustomCell.m file
MyCustomCell.m
#import "MyCustomCell.h"
#implementation MyCustomCell
#synthesize logoOn;
#synthesize logoOff;
#synthesize logo;
#synthesize name;
#synthesize divider;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.logo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.logo.userInteractionEnabled = NO;
self.logo.selected = FALSE;
self.name = [[UILabel alloc]initWithFrame:CGRectZero];
[self.name setFont:[UIFont fontWithName:#"CenturyGothic" size:14]];
self.name.numberOfLines = 2;
self.name.backgroundColor = [UIColor clearColor];
[self.name sizeToFit];
self.name.textColor = [UIColor whiteColor];
self.divider = [[UIImageView alloc]init];
[self.contentView addSubview:logo];
[self.contentView addSubview:name];
[self.contentView addSubview:divider];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)layoutSubviews
{
[super layoutSubviews];
// set the frames for all subviews
[self.logo setBackgroundImage:self.logoOff forState:UIControlStateNormal];
[self.logo setBackgroundImage:self.logoOn forState:UIControlStateSelected];
[self.logo setBackgroundImage:self.logoOn forState:UIControlStateHighlighted];
self.logo.frame = CGRectMake(20, 5, self.logoOff.size.width/2, self.logoOff.size.height/2);
self.name.frame = CGRectMake(70,13, self.bounds.size.width-70, 20);
self.divider.frame = CGRectMake(0, 43, self.bounds.size.width ,2);
}
#end
and in the controller
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CELL"];
if(cell == nil)
{
cell = [[MyCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CELL"];
}
cell.logoOff = [UIImage imageNamed:[logoimg objectAtIndex:indexPath.row*2]];
cell.logoOn = [UIImage imageNamed:[logoimg objectAtIndex:indexPath.row*2]];
cell.name.text = [logoname objectAtIndex:indexPath.row];
// cell.divider.image = [UIImage imageNamed:#"divider_line.png"]; //for test i commented
cell.divider.backgroundColor = [UIColor blackColor];
return cell;
}
Hope this helps u :)
Related
I am beginner in iOS and in my project I have create One UITableView and I have added here headerview for TableList and inside of headerView I have added one UILabel and one UIbutton ok that's fine
And when I load tableList I am loading headerView "label" text is empty and when I tapped on button which was added on my tableList headerView then I am loading UIlabel text as something in my Button action
My main problem is when I load tableList at firstTime TableList is fitting perfectly as like my below "first image" and when I tapped on button then tableList is not fitting perfectly as like my second image. Why tableList frame is changing?
My code:
#import "ViewController.h"
#interface ViewController (){
UILabel *label;
NSArray * mainArray;
NSString * HeaderlabelText;
UIView * ContentView;
}
#end
#implementation ViewController {
}
- (void)viewDidLoad{
[super viewDidLoad];
[self createTableList];
}
-(void)createTableList{
[MaintableView removeFromSuperview];
MaintableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//MaintableView.translatesAutoresizingMaskIntoConstraints = NO;
MaintableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
MaintableView.dataSource=self;
MaintableView.delegate=self;
MaintableView.scrollEnabled = YES;
[MaintableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
[self.view addSubview:MaintableView];
mainArray = [[NSArray alloc]initWithObjects:#"1",#"2",#"3", nil];
}
//Delegate methods:-
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return mainArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [MaintableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath] ;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
label = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 300, 30)];
label.textColor = [UIColor blackColor];
label.text = [mainArray objectAtIndex:indexPath.row];
label.font = [UIFont systemFontOfSize:15.0];
[cell.contentView addSubview:label];
UIView *bgview = [[UIView alloc] init];
bgview.backgroundColor = [UIColor darkGrayColor];
cell.selectedBackgroundView = bgview;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)sectio{
return 50;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 30.0f)];
[view setBackgroundColor:[UIColor redColor]];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, 50, 20)];
[lbl setFont:[UIFont systemFontOfSize:18]];
[lbl setTextColor:[UIColor whiteColor]];
lbl.text = HeaderlabelText;
[view addSubview:lbl];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self
action:#selector(PopOverAction:)
forControlEvents:UIControlEventTouchUpInside];
[button1 setTitle:#"Show View1" forState:UIControlStateNormal];
button1.backgroundColor = [UIColor orangeColor];
button1.frame = CGRectMake(80, 5, 50, 30);
[view addSubview:button1];
return view;
}
- (void)PopOverAction:(id)sender{
HeaderlabelText = #"Section1";
[self createTableList];
}
firstimage:
secondimage:
There is a best way to do what you want.
This method -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section is used to setup one header for each section. I think you need to remove it.
You want one header for all you tableView. So create a custom UIVIew variable (self.headerView) you can retain in your ViewController (the view currently created in viewForHeaderInSection method).
After that create a method like upadateHeaderView that update the label of your header. Call this method in your button action method (and not recreate tableView).
To finish when you create your tableView (only once) call [tableView setTableHeaderView:self.headerView];in your -(void)createTableList method. Now you have one header for all your tableView.
So you need to create a customView. Here an example header:
#interface CustomView : UIVIew
#property(nonatomic, strong) UILabel *label;
#property(nonatomic, strong) UIButton *button;
#end
And your viewController become like this :
#import "ViewController.h"
#interface ViewController (){
UILabel *label;
NSArray * mainArray;
NSString * HeaderlabelText;
UIView * ContentView;
CustomView *headerView;
}
#end
#implementation ViewController {
}
- (void)viewDidLoad{
[super viewDidLoad];
[self createHeader];
[self createTableList];
}
-(void)createTableList{
MaintableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
MaintableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
MaintableView.dataSource=self;
MaintableView.delegate=self;
MaintableView.scrollEnabled = YES;
[MaintableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
[MaintableView setTableHeaderView:self.headerView];
[self.view addSubview:MaintableView];
mainArray = [[NSArray alloc]initWithObjects:#"1",#"2",#"3", nil];
}
- (void)createHeaderView {
self.headerView = [CustomView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 30.0f)];
[self.headerView.button addTarget:self
action:#selector(PopOverAction:)
forControlEvents:UIControlEventTouchUpInside];
//Setup your customView HERE
}
- (void)updateHeader {
//Update self.headerView HERE
[self.headerView.label setText:HeaderlabelText];
}
//Delegate methods:-
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return mainArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [MaintableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath] ;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
label = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 300, 30)];
label.textColor = [UIColor blackColor];
label.text = [mainArray objectAtIndex:indexPath.row];
label.font = [UIFont systemFontOfSize:15.0];
[cell.contentView addSubview:label];
UIView *bgview = [[UIView alloc] init];
bgview.backgroundColor = [UIColor darkGrayColor];
cell.selectedBackgroundView = bgview;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (void)PopOverAction:(id)sender{
HeaderlabelText = #"Section1";
[self updateHeader];
}
Hi I am very new in iOS and I have created one tablelist on my Viewcontroller and here on my tablelist row I have added one UIlabel programatically, ok that's fine.
And I have added one UIbutton programatically on my "self.view".
Here my main requirement is when I click that button, I want to change UIlabel background color which was added on my tablelist row.
For this I have tried the code bellow, but UIlabel backgroudcolor is not changing.
Please help me.
my code:-
#import "ViewController.h"
#interface ViewController (){
UILabel *label;
}
#end
#implementation ViewController {
UITableView *tableView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self tablelistCreation];
}
//UItableView createtion
-(void)tablelistCreation{
tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, 300, self.420) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
//Delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];
label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
return cell;
}
//UIbutton createtion
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button .backgroundColor = [UIColor orangecolor];
button.frame = CGRectMake(100, 440, 30, 30);
[self.view addSubview:button];
//button action
-(void)Method:(id)sender{
label.backgroundColor = [UIColor redcolor];
}
Your question was not clear if you want to change the label color on all cell or just some.
Anyway this color change must be made cellForRowAtIndexPath which is where the update / creation of the cell is made.
You can create a variable to inform if you want the color change or not and it check this on cellForRowAtIndexPath.
#implementation ViewController {
UITableView *tableView;
bool changeColor;
}
-(void)Method:(id)sender{
changeColor != changeColor;
[tableview reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];
UILabel label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
[cell.contentView addSubview:label];
}
if (changeColor){
label.backgroundColor = [UIColor redcolor];
}else{
label.backgroundColor = [UIColor clearColor];
}
return cell;
}
if you want change especific labels, you can create a nsarray to control what cell will be change
#interface ViewController (){
UILabel *label;
NSMutableArray *lableArray;
}
#end
#implementation ViewController {
UITableView *tableView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self tablelistCreation];
lableArray=#[].mutableCopy;
}
//UItableView createtion
-(void)tablelistCreation{
tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, 300, self.420) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
//Delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
[lableArray addObject:label];
}
cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];
//
// label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
// label.backgroundColor = [UIColor clearColor];
// [cell.contentView addSubview:label];
return cell;
}
//UIbutton createtion
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button .backgroundColor = [UIColor orangecolor];
button.frame = CGRectMake(100, 440, 30, 30);
[self.view addSubview:button];
//button action
-(void)Method:(id)sender{
[lableArray makeObjectsPerformSelector:#selector(setBackground:) withObject:[UIColor redColor]];
// label.backgroundColor = [UIColor redcolor];
}
cell is Reusable so creation should put in cell == nil condition,at every run into the condition will creation a new UILabel ,if you want change all of them ,you should put them in a collection ,such as NSMutableArray...
import "ViewController.h"
#interface ViewController ()
{
UILabel *label;
BOOL isTouchButton;
}
#end
#implementation ViewController {
UITableView *tableView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//UIbutton createtion
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button .backgroundColor = [UIColor orangecolor];
button.frame = CGRectMake(100, 440, 30, 30);
[self.view addSubview:button];
[self tablelistCreation];
}
//UItableView createtion
-(void)tablelistCreation{
tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, 300, self.420) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
//Delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];
label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
label.backgroundColor = [UIColor clearColor];
label.tag = indexPath.row;
if(!isTouchButton)
{
cell.labelUserName.backgroundColor = [UIColor clearColor];
}
else{
cell.labelUserName.backgroundColor = [UIColor redColor];
}
[cell.contentView addSubview:label];
return cell;
}
//button action
-(void)Method:(id)sender
{
if(!isTouchButton)
{
[tablelistCreation reloadData];
isTouchButton = YES;
}
else{
[tablelistCreation reloadData];
isTouchButton = NO;
}
}
I have one "+" button in UIView. My requirement is, if i click that button UITextFields displayed in UITableViewCells. I am having idea how to display the UITextFields in UIView if user clicks the "+" button. But i dont have any idea how to display UITextFields inside of UITableViewCells if user hits the "+" button. Please kindly help me anybody. Yesterday i strucked with this functionality. I tried some code. But it is not worked properly. But i had not found any solution. Thanks in advance.
UIButton *myGreenIconButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[myGreenIconButton1 addTarget:self action:#selector(GreenIconButtonClicked)forControlEvents:UIControlEventTouchUpInside];
[myGreenIconButton1 setBackgroundImage:[UIImage imageNamed:#"index.jpg"] forState:UIControlStateNormal];
myGreenIconButton1.backgroundColor = [UIColor clearColor];
myGreenIconButton1.frame = CGRectMake(285, 144, 25, 25);
[self.view addSubview:myGreenIconButton1];
-(void)GreenIconButtonClicked
{
view=[[UIView alloc]initWithFrame:CGRectMake(0, 80, 300, 20)];
text1=[[UITextField alloc]initWithFrame:CGRectMake(10, 80, 100, 20)];
text1.borderStyle=UITextBorderStyleRoundedRect;
text1.backgroundColor=[UIColor clearColor];
text1.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
text1.font=[UIFont systemFontOfSize:14.0];
text1.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
text1.textAlignment=NSTextAlignmentCenter;
text1.delegate=self;
[view addSubview:text1];
text2=[[UITextField alloc]initWithFrame:CGRectMake(120, 80, 100, 20)];
text2.borderStyle=UITextBorderStyleRoundedRect;
text2.backgroundColor=[UIColor clearColor];
text2.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
text2.font=[UIFont systemFontOfSize:14.0];
text2.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
text2.textAlignment=NSTextAlignmentCenter;
text2.delegate=self;
[view addSubview:text2];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
[cell.textLabel setText:#""];
[view setTag:101];
NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell *cell1 = [self.tableView cellForRowAtIndexPath:ip];
UITextField *textField = (UITextField*)[cell1 viewWithTag:101];
UITextField *textField1=(UITextField*)[cell1 viewWithTag:101];
UITextField *textField2=(UITextField*)[cell1 viewWithTag:101];
[cell.contentView addSubview:text1];
[cell.contentView addSubview:text2];
[cell.contentView addSubview:text3];
return cell;
}
I have created a example for your requirement and posted it on github. You can find code Here
I have created a custom table view cell with text field on it.
First create custom cell with xib or without and do following manner its working for you :
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell {
}
#property (strong,nonatomic) IBOutlet UITextField *textField;
#end
CustomCell.m
#import "CustomCell.h"
#implementation CustomCell
#synthesize textField;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.textField = [[UITextField alloc] init];
[self.contentView addSubview:self.textField];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
Your TableView .h
#property (strong,nonatomic) NSMutableArray *array;
#property (strong, nonatomic) IBOutlet UITableView *tblView;
Your Table View .m
#synthesize array;
#synthesize tblView;
-(void)viewWillAppear:(BOOL)animated {
array = [[NSMutableArray alloc] init];
[tblView reloadData];
}
-(int)numberOfSectionsInTableView:(UITableView *)tableView
{
if( [array count ] > 0 )
{
return 1;
}
return 0;
}
-(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if( [array count ] > 0 )
{
return [array Count];
}
return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat:#"CustomCell%d%d",indexPath.row,indexPath.section];
[tblCreateProfile registerNib:[UINib nibWithNibName:#"CustomCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if(cell == nil)
{
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cellIdentifier =nil;
cell.textField.text = [array objectAtIndex:indexPath.row];
return cell;
}
-(void)GreenIconButtonClicked
{
[array addObject:#"Test"];
[tblView reloadData];
}
So just try in Custom TableViewCells. So that You can Achieve the solution for this problem.
I want to implement a SwipeCell with SWTableViewCell Library. I want to create a subclass of SWTableViewCell to customize it easier. I have done it, but it does not work. I guess I am forgetting something, I mean, swipe is not working but I can see the cell.
Here is my subclass code:
.h
#import <UIKit/UIKit.h>
#import "SWTableViewCell.h"
#interface WPUserPlanCell : SWTableViewCell
#property (strong, nonatomic)WPCustomLabel *tileLabel;
#property (strong, nonatomic)WPCustomLabel *dateLabel;
#property (nonatomic) id <SWTableViewCellDelegate> delegate;
-(void)configureCellWithTitle:(NSString *)title andDate:(NSString *)date;
#end
.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
UITableView * table= nil;
WPUserPlanCell *cell = (WPUserPlanCell *)self;
if([cell isKindOfClass:[UITableViewCell class]]){
table = (UITableView *)cell.superview;
if(IS_OS_7_OR_LATER){
table = (UITableView *)cell.superview.superview;
}
}
NSMutableArray *rightUtilityButtons = [NSMutableArray new];
[rightUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f] title:#"Delete"];
self = [super initWithStyle:style
reuseIdentifier:reuseIdentifier
containingTableView:table
leftUtilityButtons:nil
rightUtilityButtons:rightUtilityButtons];
if (self)
{
CGRect labelFrame = UIEdgeInsetsInsetRect(self.bounds, titleLabelInsets);
labelFrame.size = kLabelSize;
tileLabel = [[WPCustomLabel alloc] initWithFrame:labelFrame];
[tileLabel setTextColor:[UIColor blackColor]];
[tileLabel setFont:[UIFont systemFontOfSize:14]];
[tileLabel setBackgroundColor:[UIColor clearColor]];
tileLabel.textAlignment = NSTextAlignmentLeft;
[self addSubview:tileLabel];
labelFrame.size.width = self.bounds.size.width - kLabelSize.width;
labelFrame.origin.x = CGRectGetMaxX(labelFrame) + 15;
dateLabel = [[WPCustomLabel alloc] initWithFrame:labelFrame];
[dateLabel setTextColor:[UIColor orangeColor]];
[dateLabel setFont:[UIFont systemFontOfSize:14]];
[dateLabel setBackgroundColor:[UIColor clearColor]];
dateLabel.textAlignment = NSTextAlignmentRight;
[self addSubview:dateLabel];
}
return self;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (aTableView == self.tableView) {
Plan *plan = (Plan *)[self.fetchedResultsController objectAtIndexPath:indexPath];
// Creating Reusable Cell
WPUserPlanCell *cell = (WPUserPlanCell *)[aTableView dequeueReusableCellWithIdentifier:PlanCellIdentifier];
if (cell == nil)
{
// INIT REUSABLE CELL
cell = [[WPUserPlanCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PlanCellIdentifier] ;
cell.delegate = self;
}
[cell configureCellWithTitle:plan.name andDate:plan.date];
return cell;
}
return nil;
}
You can try
[cell setCellHeight:_customHeight];
if your cell height is not default value.
I'm creating the cells of a UITableViewController.
In one of them, a small image is contained. I tried the following to make its corners rounded:
profileImage.layer.cornerRadius = 8;
profileImage.clipsToBounds = YES;
In another cell prototype, I tried to make the corners of a button rounded:
chooseImageFromRoll.clipsToBounds = YES;
chooseImageFromRoll.layer.cornerRadius = 8;
In both cases I included
#import <QuartzCore/QuartzCore.h>
The button and the image whose corners must be rounded are property of the UITableViewController owning them:
#import <UIKit/UIKit.h>
#interface profileRegistrationCellVC : UITableViewCell
#property (weak, nonatomic) IBOutlet UIImageView *profileImage;
#property (weak, nonatomic) IBOutlet UIButton *chooseImageFromRoll;
#property (weak, nonatomic) IBOutlet UIButton *shootImage;
#end
In relative .m class:
#import "profileRegistrationCellVC.h"
#import <QuartzCore/QuartzCore.h>
#implementation profileRegistrationCellVC
#synthesize profileImage;
#synthesize chooseImageFromRoll;
#synthesize shootImage;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
chooseImageFromRoll.clipsToBounds = YES;
chooseImageFromRoll.layer.cornerRadius = 8;
shootImage.layer.cornerRadius = 8;
profileImage.layer.cornerRadius = 8;
profileImage.clipsToBounds = YES;
profileImage.layer.borderColor = [UIColor whiteColor].CGColor;
profileImage.layer.borderWidth = 20.0;
[self addSubview:profileImage];
[self addSubview:chooseImageFromRoll];
[self addSubview:shootImage];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
Here's the code of my function cellForRowAtIndexPath, in my uitableviewcontroller:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
static NSString *CellIdentifier = #"profileRegistrationCell";
profileRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[profileRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//cell.profileImage.image.layer.masksToBounds = YES;
//cell.profileImage.layer.cornerRadius = 5.0;
return cell;
}
else if (indexPath.section == 1) {
static NSString *CellIdentifier = #"regularRegistrationCell";
regularRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[regularRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.regularFieldName.text = [_registrationItems objectAtIndex:indexPath.row];
if ([[_registrationItems objectAtIndex:indexPath.row] isEqualToString:#"Email*"])
cell.regularTextField.keyboardType = UIKeyboardTypeEmailAddress;
if ([[_registrationItems objectAtIndex:indexPath.row] isEqualToString:#"Età "]) {
cell.regularTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
}
return cell;
}
else{
static NSString *CellIdentifier = #"orientationRegistrationCell";
orientationRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[orientationRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.fieldLabel.text = [_registrationItems objectAtIndex:[_registrationItems count]-1];
cell.orientationLabel.text = #"Non specificato";
return cell;
}
}
But in no case I managed to make corners rounded. Can you tell where I'm mistaking?
Thanks
The code snippets above are both correct and have no issues. My assumption is that your issue lies elsewhere in creating the button that contains the image. The following code will create a button and will round it's corners, also I added the border to it in case you want to add that too. You can plug in the image you have in there as well. I have added a image of what this code will create for your reffrence also. Hope it will help you out.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100,50);
[btn setTitle:#"Hello" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor colorWithRed:128.0/255.0f green:0.0/255.0f blue:0.0/255.0f alpha:0.7]];
btn.frame = CGRectMake(100.0, 100.0, 120.0, 50.0);//width and height should be same value
btn.clipsToBounds = YES;
btn.layer.cornerRadius = 20;//half of the width
btn.layer.borderColor=[UIColor redColor].CGColor;
btn.layer.borderWidth=2.0f;
[self.view addSubview:btn];
Below is the image of the button that is related with the above code
Edit:
another way of doing the round corners is to use the method masksToBounds here is an example of it within the generic cell right out of the box from the template.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
cell.imageView.image = [UIImage imageNamed:#"acolon.png"];
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 5.0;
return cell;
}
here is the screen shot of the result:
i know you using a custom cell, so implement the maskToBount in the cellForRowAtImdexPath or wherever you are populating the tableview with its custom cells.