iOS How to create undefined UIControl in custom UITableViewCell - ios

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];
...
}

Related

UITableView Frame is not fitting on self.view when i tapped on Button in ios

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];
}

How to change TableList UILabel backGroundColor when we tapped on UIbutoon?

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;
}
}

Incorrect UITableViewCell gets highlighed when dequeued

I have written a subclass of UITableViewCell to allow horizontal swipe to give some actions to users. Here is what I am doing:
Create a scrollView
Create a buttonView and add in scrollView.
Create a UIButton and add all cell controls as subview to it. Add in scroll view.
Add scrollView to cell contentView.
For #3 I am setting the highlighted image to give a feel of user tap like in normal cell.
The issue is when my table view is loaded on iOS 6 with 6 cells and user tap on any of the cell, cell gets highlighted properly and the details are shown properly for the tapped cell. But if user scrolls up and first cell is re-used and user tap on the top cell (which is second row), cell next to it gets highlighted. If user scrolls up and purge 2 cells and tap on the top cell, cell 2 cells down it gets highlighted. Although tapped cell shows the data of the correct cell.
Any clue?
- (id)initWithStyle:(UITableViewCellStyle)iStyle reuseIdentifier:(NSString *)iReuseIdentifier andMenuButtonDetails:(NSArray *)iMenuButtonDetails {
if ((self = [super initWithStyle:iStyle reuseIdentifier:iReuseIdentifier])) {
self.catchWidth = kMenuButtonWidth * [iMenuButtonDetails count];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(kScreenOrigin, kScreenOrigin, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.bounds) + self.catchWidth, CGRectGetHeight(self.bounds));
self.scrollView.delegate = self;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.scrollEnabled = YES;
[self.contentView addSubview:self.scrollView];
self.scrollViewButtonView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.bounds) - self.catchWidth, kScreenOrigin, self.catchWidth, CGRectGetHeight(self.bounds))];
[self.scrollView addSubview:self.scrollViewButtonView];
if ([iMenuButtonDetails count]) {
// Adding menu buttons to the cell.
CGFloat anXOffset = kScreenOrigin;
for (NSDictionary *aMenuButton in iMenuButtonDetails) {
if ([aMenuButton containsObjectForKey:kTitleKey]) {
UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(anXOffset, kScreenOrigin, kMenuButtonWidth, kCellHeight64)];
[aButton addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside];
if ([aMenuButton containsObjectForKey:kButtonTagKey])
aButton.tag = [[aMenuButton stringForKey:kButtonTagKey] intValue];
aButton.titleEdgeInsets = UIEdgeInsetsMake(kScreenOrigin, 2.0, kScreenOrigin, 2.0);
aButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
[aButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
[aButton setTitle:[aMenuButton stringForKey:kTitleKey] forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
if ([aMenuButton objectForKey:kButtonColorKey]) {
aButton.backgroundColor = [aMenuButton objectForKey:kButtonColorKey];
}
[self.scrollViewButtonView addSubview:aButton];
anXOffset += kMenuButtonWidth;
}
}
}
self.scrollViewContentView = [UIButton buttonWithType:UIButtonTypeCustom];
self.scrollViewContentView.frame = CGRectMake(kScreenOrigin, kScreenOrigin, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
if (![Utilities isIOS7orAbove]) {
[self.scrollViewContentView addTarget:self action:#selector(cellHighlighted) forControlEvents:UIControlEventTouchDown];
[self.scrollViewContentView addTarget:self action:#selector(cellCancelHighlight) forControlEvents:UIControlEventTouchDragExit];
}
[self.scrollViewContentView addTarget:self action:#selector(selectCell:) forControlEvents:UIControlEventTouchUpInside];
self.scrollViewContentView.backgroundColor = [UIColor whiteColor];
UIImage *aBGHighlightedImage = nil;
if ([Utilities isIOS7orAbove]) {
aBGHighlightedImage = [UIImage imageNamed:kCellHighlightedImageIOS7];
} else {
aBGHighlightedImage = [UIImage imageNamed:kCellHighlightedImageIOS6];
}
[self.scrollViewContentView setBackgroundImage:[aBGHighlightedImage stretchableImageWithLeftCapWidth:11.0f topCapHeight:0] forState:UIControlStateHighlighted];
[self.scrollView addSubview:self.scrollViewContentView];
[self.scrollViewContentView addSubview:self.imageView];
[self.scrollViewContentView addSubview:self.textLabel];
[self.scrollViewContentView addSubview:self.detailTextLabel];
}
- (void)prepareForReuse {
[super prepareForReuse];
self.scrollViewContentView.enabled = YES;
[self.scrollView setContentOffset:CGPointZero animated:NO];
}
- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {
MyTableViewCell *aCell = (MyTableViewCell *)[iTableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (!aCell) {
aCell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"CellIdentifier" andMenuButtonDetails:aMenuButtons];
}
// Set data on cell now
return aCell
}
Let me know if there is something I'm missing here, but it seems like you're adding a ton of complexity to your class for no reason. Are you familiar with UICollectionView?
Here's an example implementation (which scrolls horizontally):
#interface asdf () <UICollectionViewDataSource, UICollectionViewDelegate>
#property (strong, nonatomic) UICollectionView *collectionView;
#end
#implementation asdf
- (id)init
{
self = [super init];
if (self)
{
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.collectionViewLayout];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.view addSubview:self.collectionView];
NSString *className = NSStringFromClass([UICollectionViewCell class]);
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:className];
}
return self;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.collectionView.frame = self.view.bounds;
}
- (UICollectionViewLayout *)collectionViewLayout
{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 0;
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
return layout;
}
#pragma mark - UICollectionView
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 5;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSString *className = NSStringFromClass([UICollectionViewCell class]);
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:className forIndexPath:indexPath];
// This is for dequeuing
NSInteger tag = 12324;
UIView *view = [cell viewWithTag:tag];
if (view)
[view removeFromSuperview];
view = [[UIView alloc] initWithFrame:cell.bounds];
view.tag = tag;
// Add all of your subviews to the view property
[cell addSubview:view];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(collectionView.bounds.size.width, 50);
}
#end
I wrote this quickly as a sample, so it's not tailored specifically to what you're building, but this should give you a nice idea of how simple it is to implement a UICollectionView.
This answer may come across as random for what you're asking, but when possible, you should always try to use what Apple provides over what you would spend precious hours re-inventing the wheel w/ & likely experience random nuances like yours.

Custom UITableViewCell functions are called, but native table is presented

Custom UITableViewCell functions are called, but native table is presented
This is my code in the init function:
self.answersTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 45, 280, 500)];
self.answersTable.dataSource = self;
self.answersTable.delegate = self;
This is my code in the viewDidLoad function:
[self.answersTable registerClass:[AnswerTableViewCell class]
forCellReuseIdentifier:CellIdentifier];
This is my table delegate functions, which are called, and the cell is not nil in the end of it:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.question.answers count];
}
-(AnswerTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AnswerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[AnswerTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
AnswerObject* answer = self.question.answers[indexPath.row];
[cell setupAnswerTableViewCell:self.question answer:answer row:indexPath.row];
return cell;
}
And here is AnswerTableViewCell:
#import "AnswerTableViewCell.h"
#implementation AnswerTableViewCell
- (id)init
{
self = [super init];
if (self) {
self.frame = CGRectMake(0, 0, 280, 77);
self.answerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width-20, self.frame.size.height)];
self.answerLabel.numberOfLines = 0;
self.answerLabel.textColor = [UIColor whiteColor];
self.answerLabel.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:20];
self.answerToggle = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width-50, 23, 30, 30)];
self.backgroundImage = [[UIImageView alloc]initWithFrame:self.frame];
[self addSubview:self.answerLabel];
[self addSubview:self.answerToggle];
[self addSubview:self.backgroundImage]; }
return self;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.frame = CGRectMake(0, 0, 280, 77);
self.answerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width-20, self.frame.size.height)];
self.answerLabel.numberOfLines = 0;
self.answerLabel.textColor = [UIColor whiteColor];
self.answerLabel.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:20];
self.answerToggle = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width-50, 23, 30, 30)];
self.backgroundImage = [[UIImageView alloc]initWithFrame:self.frame];
[self addSubview:self.answerLabel];
[self addSubview:self.answerToggle];
[self addSubview:self.backgroundImage]; }
return self;
}
-(void)setupAnswerTableViewCell:(QuestionObject*)question
answer:(AnswerObject*)answer
row:(NSInteger)row{
self.question = question;
self.answer = answer;
self.row = row;
self.answerLabel.text = answer.answerText;
[self.answerToggle addTarget:self
action:#selector(flip:)
forControlEvents:UIControlEventTouchDown];
self.answerToggle.tag = [answer.answerID intValue];
if (self.row == 0) {
self.backgroundImage.image = [UIImage imageNamed:#"List_Top_Item_Not_Selected_612x113px.png"];
}
else if (self.row == ([self.question.answers count] - 1)){
self.backgroundImage.image = [UIImage imageNamed:#"List_Bottom_Item_Not_Selected_612x113px.png"];
}
else{
self.backgroundImage.image = [UIImage imageNamed:#"List_Item_Not_Selected_612x113px.png"];
}
}
So, whats wrong?
The problem is:
cell = [[AnswerTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
specifically: UITableViewCellStyleDefault
You're asking it to create a Cell with the default style. Either just use [[AnswerTableViewCell alloc] init]; and set the reuse identifier. Or create your own init that takes in a reuse identifier without a style param
EDIT:
After the Cell code was added I believe the issue to be that you are using the init function to add your UI elements. These are most likely being overridden by the cells UIView contentView. Its the contentView you need to customise in order to add these elements.
This article will walk you through how to do this and using the drawRect method to customise the UIView.
http://blog.giorgiocalderolla.com/2011/04/16/customizing-uitableviewcells-a-better-way/
As stupid as it may sound, the thing that fixed it for me was:
self.answersTable.backgroundColor = [UIColor clearColor];

How to subclass SWTableViewCell library programmatically? (Swipe cells)

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.

Resources