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.
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];
}
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 :)
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'm trying to write a prototype UITableViewCell with title and undefined UIControl that we are setting in cellForRowAtIndexPath:
Now I have implementation for cell:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self.contentView addSubview:self.titleLabel];
[self.contentView addSubview:self.control];
}
return self;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
[_titleLabel setBackgroundColor:[UIColor clearColor]];
_titleLabel.numberOfLines = 2;
[_titleLabel setFont:[UIFont fontWithName:#"Georgia-Italic" size:15]];
[_titleLabel setTextColor:RGBColor(51, 102, 153)];
[_titleLabel setTextColor:[UIColor darkGrayColor]];
}
return _titleLabel;
}
- (UIControl *)control{
if (!_control) {
_control = [[UIControl alloc] init];
[_control setBackgroundColor:[UIColor whiteColor]];
}
return _control;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = [[self contentView] bounds];
self.titleLabel.frame = CGRectMake(10, 5, 85, contentRect.size.height-10);
self.control.frame = CGRectMake(100, 5, contentRect.size.width-105, contentRect.size.height-10);
[self.contentView bringSubviewToFront:self.control];
[self bringSubviewToFront: self.control];
}
And implemented cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellAddOrder";
ICControlCell *cell = (ICControlCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ICControlCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
[cell setAccessoryType:UITableViewCellAccessoryNone];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
NSObject *cellContent = [self.cellList objectAtIndex:indexPath.row];
cell.titleLabel.text = ICLocalized([cellContent valueForKey:#"name"]);
switch ([[cellContent valueForKey:#"control"] intValue]) {
case 0: {
UITextField *textField = [[UITextField alloc] init];
textField.placeholder = ICLocalized([[cellContent valueForKey:#"name"] stringByAppendingString:#"_placeholder"]);
textField.delegate = self;
textField.returnKeyType = UIReturnKeyNext;
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.text = [cellContent valueForKey:#"data"];
textField.tag = indexPath.row;
cell.control = textField;
}
break;
case 1:
case 2:
case 3: {
UISwitch *switchControl = [[UISwitch alloc] init];
[switchControl setOn:[[cellContent valueForKey:#"data"] boolValue]];
[switchControl addTarget:self
action:#selector(switchStateChanged:)
forControlEvents:UIControlEventValueChanged];
switchControl.tag = indexPath.row;
cell.control = switchControl;
}
break;
default:
break;
}
return cell;
}
But when I'm trying to test that controller controls for cell are not displaying, just title labels..
How can I handle that problem? Please help me
you should use different cell identifiers for different items for example if you have UITextField and UISwitch then you should use two identifiers because you are reusing the cell.So it should be the same type of cell to be reused.
Doc says:
You cannot use the UIControl class directly to instantiate controls.
So, you can just declare the UIControl variable the cell prototype class but set it to nil there. All the control related operations should be done from inside the delegate, because that is the place where you exactly know which class to use. So,
Don't initialize control in cell class snippet from 'control' property:
- (UIControl *)control{
return _control;
}
Cell's constructor should also not call addSubview for control. So,
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self.contentView addSubview:self.titleLabel];
}
return self;
}
Add background color setting in cell's layoutSubviews here:
- (void)layoutSubviews {
...
if (cell.control) {
[cell.control setBackgroundColor:[UIColor whiteColor]];
self.control.frame = CGRectMake(100, 5, contentRect.size.width-105, contentRect.size.height-10);
[self.contentView bringSubviewToFront:self.control];
[self bringSubviewToFront: self.control];
}
}
Add subview in the delegate's cellforrow method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
cell.control = textField;
[cell.contentView addSubview:cell.control];
...
}
I have a subclass of UITableViewCell, and I am having problems with scrolling. All of the subviews are added to the cell in the storyboard, except for one UIView. I want to add this UIView as a subview of the cell based on a condition. The problem is that when the cells are scrolled off and onto the screen, the UIView is added to the cell a second time, or to the wrong cell. Here is my code, can you tell me what I am doing wrong?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
WavesFeedCell *cell = (WavesFeedCell *)[tableView dequeueReusableCellWithIdentifier:WavesFeedCellIdentifier forIndexPath:indexPath];
cell.cellWaveObject = [self.wavesArray objectAtIndex:indexPath.row];
//set the frames and text for the labels
cell.timeStampLabel.text = #"14m";
cell.waveTextLabel.text = cell.cellWaveObject.waveString;
cell.wavedByLabel.text = [NSString stringWithFormat:#"Waved by %#", cell.cellWaveObject.wavedByString];
//round the corners of the image view
[self setCornerRadiusForImageView:cell.profilePictureImageView];
//does the wave object have any agrees?
if (cell.cellWaveObject.numberOfAgrees > 0)
{
UIView *agreedView = [[UIView alloc] init];
UILabel *numberOfAgreesLabel = [[UILabel alloc] init];
numberOfAgreesLabel.font = [UIFont boldSystemFontOfSize:13.0f];
numberOfAgreesLabel.textColor = [UIColor whiteColor];
if (cell.cellWaveObject.numberOfAgrees > 1)
{
numberOfAgreesLabel.text = [NSString stringWithFormat:#"+%i Agree", cell.cellWaveObject.numberOfAgrees];
}
else
{
numberOfAgreesLabel.text = [NSString stringWithFormat:#"+%i Agrees", cell.cellWaveObject.numberOfAgrees];
}
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:AgreedViewImage]];
//get the width of the string
CGSize constraintSize = CGSizeMake(242.0f, 16.0f);
CGSize stringSize = [numberOfAgreesLabel.text sizeWithFont:numberOfAgreesLabel.font constrainedToSize:constraintSize];
CGFloat agreedViewWidth = stringSize.width + 10.0f;
//adjust the frame and add it to the cell
agreedView.frame = CGRectMake(310.0f - agreedViewWidth, cell.wavedByLabel.frame.origin.y, agreedViewWidth, 14.0f);
backgroundImageView.frame = CGRectMake(5.0f, 0.0f, agreedView.frame.size.width, agreedView.frame.size.height);
numberOfAgreesLabel.frame = CGRectMake(5.0f, 0.0f, agreedView.frame.size.width, agreedView.frame.size.height);
[agreedView addSubview:backgroundImageView];
[agreedView addSubview:numberOfAgreesLabel];
[cell.contentView addSubview:agreedView];
return cell;
}
else
{
return cell;
}
}
May be added subview is not removing from cell.Content View .
Try using following code within cellForRow method
UIView *agreedView=(UIView*)[cell.contentView viewWithTag:21];
//does the wave object have any agrees?
if (cell.cellWaveObject.numberOfAgrees > 0)
{
if (!agreedView) {
agreedView=[[UIView alloc] init];
agreedView.tag=21;
UILabel *numberOfAgreesLabel = [[UILabel alloc] init];
numberOfAgreesLabel.font = [UIFont boldSystemFontOfSize:13.0f];
numberOfAgreesLabel.textColor = [UIColor whiteColor];
numberOfAgreesLabel.tag=22;
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:AgreedViewImage]];
backgroundImageView.tag=23;
[agreedView addSubview:backgroundImageView];
[agreedView addSubview:numberOfAgreesLabel];
[cell.contentView addSubview:agreedView];
}
numberOfAgreesLabel=(UILabel*)[agreedView viewWithTag:22];
backgroundImageView=(UIImageView*)[agreedView viewWithTag:23];
if (cell.cellWaveObject.numberOfAgrees > 1)
{
numberOfAgreesLabel.text = [NSString stringWithFormat:#"+%i Agree", cell.cellWaveObject.numberOfAgrees];
}
else
{
numberOfAgreesLabel.text = [NSString stringWithFormat:#"+%i Agrees", cell.cellWaveObject.numberOfAgrees];
}
//get the width of the string
CGSize constraintSize = CGSizeMake(242.0f, 16.0f);
CGSize stringSize = [numberOfAgreesLabel.text sizeWithFont:numberOfAgreesLabel.font constrainedToSize:constraintSize];
CGFloat agreedViewWidth = stringSize.width + 10.0f;
//adjust the frame and add it to the cell
agreedView.frame = CGRectMake(310.0f - agreedViewWidth, cell.wavedByLabel.frame.origin.y, agreedViewWidth, 14.0f);
backgroundImageView.frame = CGRectMake(5.0f, 0.0f, agreedView.frame.size.width, agreedView.frame.size.height);
numberOfAgreesLabel.frame = CGRectMake(5.0f, 0.0f, agreedView.frame.size.width, agreedView.frame.size.height);
return cell;
}
else
{
if (agreedView) {
[agreedView removeFromSuperview];
}
return cell;
}
Better you set frames for your custom views in subclassed UITableView cell's class
hear is the sample code
//.h file of subclassed UITableView cell
#import <UIKit/UIKit.h>
#interface WavesFeedCell : UITableViewCell
#property(nonatomic, retain)UILabel *timeStampLabel;
#property(nonatomic, retain)UILabel *waveTextLabel;
#property(nonatomic, retain)UILabel *wavedByLabel;
#property(nonatomic, retain) id cellWaveObject; //your object from array
#end
//.m file subclassed UITableView cell
#import "WavesFeedCell.h"
#implementation WavesFeedCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectZero];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectZero];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectZero];
self.timeStampLabel = label1;
self.waveTextLabel = label2;
self.wavedByLabel = label3;
//these are added with zero rect
[self addSubview:label1];
[self addSubview:label2];
[self addSubview:label3];
//for agreed imageview
UIImageView *agreedImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
agreedImgView.tag = 200; //to access in layoutsubviews.
[self addSubview:agreedImgView];
//without ARC
[agreedImgView release];
[label1 release];
[label2 release];
[label3 release];
}
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];
//hear set frames for all labels
self.timeStampLabel.frame = CGRectMake(5, 5, self.bounds.size.width-30.0f, 40.0f);
self.waveTextLabel.frame = CGRectMake(5, 45, self.bounds.size.width-30.0f, 40.0f);
self.wavedByLabel.frame = CGRectMake(5, 95, self.bounds.size.width-30.0f, 40.0f);
//hear you check weather it is agreed or not
if(self.cellWaveObject.numberOfAgrees > 1)
{
UIImageView *agreedView = (UIImageView *)[self viewWithTag:200];
//set frme for imageview as you did
// remember dont add any views hear and set your required frames only because this method called many times
}
//in other class
//.m file
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
WavesFeedCell *cell = [self.aTableView dequeueReusableCellWithIdentifier:#"WavesFeedCell"];
if(cell == nil)
{
cell = [[WavesFeedCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"WavesFeedCell"];
}
//set cell properties
return cell;
}