Here is my code , it is very sample.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
_userView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
_userView.backgroundColor = [UIColor grayColor];
[self addSubview:_userView];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, 320, 420) style:UITableViewStyleGrouped];
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.delegate = self;
_tableView.dataSource = self;
[self addSubview:_tableView];
// _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
}
return self;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"aaa"];
if (!cell.backgroundView) {
NSLog(#"wrong");
}
return cell;
}
My table is grouped , the doc of Apple says that :
the cell.background : // Default is nil for cells in UITableViewStylePlain, and non-nil for UITableViewStyleGrouped. The 'backgroundView' will be added as a subview behind all other views.
but my cell.backgroundView = nil.
anyone help , thank you.
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 am trying to create expand/collapse cell. On every cell, it has subview of UIView for border bottom. If expanded, the border should also go to bottom of the cell. It is working fine on initial load of the cells, however, when I scroll down, the border is no longer going to the bottom.
I am adjusting the border originY via taskOptionsExpand method. That method is getting the border view via [cell.contentView viewWithTag:2].
Code below:
#import "HomeViewController.h"
#interface HomeViewController ()<UITableViewDelegate, UITableViewDataSource>
#property (strong, nonatomic) UITableView *tableView;
#property (strong, nonatomic) NSIndexPath *selectedIndexPath;
#end
#implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.selectedIndexPath = nil;
[self.view addSubview:self.tableView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc] init];
_tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
[_tableView addSubview:self.tableRefreshControl];
}
return _tableView;
}
# pragma mark - table view delegates
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 100;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.selectedIndexPath isEqual:indexPath]) {
return 80;
}else{
return 50;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
// text view
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 0, self.view.frame.size.width-20, 50)];
view1.backgroundColor = [UIColor clearColor];
view1.tag = 0;
UILabel *title = [[UILabel alloc] initWithFrame:view1.frame];
title.text = #"Lorem ipsum";
title.textColor = [UIColor grayColor];
[view1 addSubview:title];
// options view
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 30)];
view2.backgroundColor = [UIColor orangeColor];
view2.tag = 1;
// border bottom
CGRect frame = CGRectMake(20, 49, self.view.frame.size.width-40, 1);
if ([self.selectedIndexPath isEqual:indexPath]) {
frame.origin.y = 79; //expand
} else {
}
UIView *border = [[UIView alloc] initWithFrame:frame];
border.backgroundColor = [UIColor blueColor];
border.tag = 2;
cell.clipsToBounds = YES;
[cell.contentView addSubview:view1];
[cell.contentView addSubview:view2];
[cell.contentView addSubview:border];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.selectedIndexPath isEqual:indexPath]) {
[self taskOptionsExpand:NO indexPath:indexPath];
self.selectedIndexPath = nil;
} else {
[self taskOptionsExpand:YES indexPath:indexPath];
[self taskOptionsExpand:NO indexPath:self.selectedIndexPath];
self.selectedIndexPath = indexPath;
}
[tableView beginUpdates];
[tableView endUpdates];
}
- (void) taskOptionsExpand:(BOOL) expand indexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIView *border = [cell.contentView viewWithTag:2];
NSLog(#"border: %#", border);
if (expand) {
border.frame = CGRectMake(20, 79, self.view.frame.size.width-40, 1);
}else{
border.frame = CGRectMake(20, 49, self.view.frame.size.width-40, 1);
}
}
#end
When scrolling the table view, the cell is dequeued to get reusable cell and as reusable cell will already have viewWithTag 2 (since it was added when the cell being reused was created) so adding another view with tag 2 will create the issues like above. To overcome the above issue you should remove the earlier added viewWithTag 2 and than re-add the view with the same tag, like-
// remove (previously added) border if it exists
UIView *border = nil;
border = [cell.contentView viewWithTag:2];
if(border)
[border removeFromSuperview];
// again create border view
Update your cellForRowAtIndexPath: method as
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
// text view
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 0, self.view.frame.size.width-20, 50)];
view1.backgroundColor = [UIColor clearColor];
view1.tag = 0;
UILabel *title = [[UILabel alloc] initWithFrame:view1.frame];
title.text = #"Lorem ipsum";
title.textColor = [UIColor grayColor];
[view1 addSubview:title];
// options view
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 30)];
view2.backgroundColor = [UIColor orangeColor];
view2.tag = 1;
// border bottom
CGRect frame = CGRectMake(20, 49, self.view.frame.size.width-40, 1);
if ([self.selectedIndexPath isEqual:indexPath]) {
frame.origin.y = 79; //expand
} else {
}
UIView *border = nil;
border = [cell.contentView viewWithTag:2];
if(border)
[border removeFromSuperview];
border = [[UIView alloc] initWithFrame:frame];
border.backgroundColor = [UIColor blueColor];
border.tag = 2;
cell.clipsToBounds = YES;
[cell.contentView addSubview:view1];
[cell.contentView addSubview:view2];
[cell.contentView addSubview:border];
// release your allocated instances after adding them
[view1 release];
[view2 release];
[border release];
return cell;
}
Also you should release your subviews which you have allocated like view1, view2, border after adding them to the cell's content view as
[view1 release];
[view2 release];
[border release];
This will make there retain count from 2 to 1 and when the cell is deallocated than they will be removed from there parent which is cell.
Table views re-use cells. When a cell is scrolled offscreen it is added to a queue, and will be re-used for the next cell to be scrolled onscreen. That means your configuration code in tableView:cellForRowAtIndexPath: method will be run again on the same cell multiple times at different indexPaths. Since you're just adding your border view every time your configuration code runs, they will stack one on top of the other. When you call viewWithTag: you will only get one of the multiple views you've added.
The correct approach is to create a custom cell (subclass of UITableViewCell) with properties for each subview that needs to be configured (IBOutlets if using xibs/storyboard), so that they can be accessed.
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];
I have a UICollectionView to display username of user, and when I add new or modify a user -> I will update into database -> then get all data again(from the data base). and then reload UICollectionView. All I want is: if I modify a user at index 3 then after reload this user is still stay at index 3(and if I add a new user this user will display at the end position). So that I use setNeedsDisplay. But I have a problem that my label display text not well when I use setNeedsDisplay,as below:
when I comment out [cell setNeedsDisplay]; then the text of label is display well. But the index of each user is not display right as I want.Here is my code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
if(!dbManager)
dbManager = [DBManager sharedInstant];
UIBarButtonItem *btnAdd = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(showAdd)];
self.navigationItem.rightBarButtonItem = btnAdd;
UIBarButtonItem *btnFilter = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(showFilter)];
self.navigationItem.leftBarButtonItem = btnFilter;
[[DBManager sharedInstant] setDelegate:self];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.collectionView setBackgroundColor:[UIColor clearColor]];
[self.collectionView registerClass:[UserCollectionItemView class] forCellWithReuseIdentifier:#"UserCollectionItemView"];
cells = [[NSMutableArray alloc] init];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (!dbManager.synchronized) {
[datasource removeAllObjects];
datasource = nil;
if (contactType == ContactTypeCustomer)
[dbManager requestData:kDbCustomers predicate:nil target:self];
else if (contactType == ContactTypeSuppplier)
[dbManager requestData:kDbSuppliers predicate:nil target:self];
}
[self setLayout];
}
and for collectionview:
#pragma mark
#pragma UICollectionDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)cv numberOfItemsInSection:(NSInteger)section
{
return [datasource count];
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
[cells addObject:cell];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UserCollectionItemView *cell;
// if([cells count])
// {
// cell = [cells lastObject];
// [cells removeLastObject];
// }
// else
cell = [cv dequeueReusableCellWithReuseIdentifier:#"UserCollectionItemView" forIndexPath:indexPath];
if (contactType == ContactTypeCustomer) {
POSCustomer *customer = [datasource objectAtIndex:indexPath.item];
cell.displayname = customer.CompanyName;
}
else if (contactType == ContactTypeSuppplier){
POSSupplier *supplier = [datasource objectAtIndex:indexPath.item];
cell.displayname = supplier.CompanyName;
}
cell.backgroundColor = [UIColor clearColor];
[cell setNeedsDisplay];
return cell;
}
- (void)collectionView:(UICollectionView *)cv didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
dbManager.synchronized = YES;
if (contactType == ContactTypeCustomer) {
POSCustomer *customers = [datasource objectAtIndex:indexPath.item];
[self showEditCustomer:customers];
}
else if (contactType == ContactTypeSuppplier){
POSSupplier *suppliers = [datasource objectAtIndex:indexPath.item];
[self showEditSupplier:suppliers];
}
}
-(void)showEditCustomer:(POSCustomer *)customer{
ContactFormViewController *form = [[ContactFormViewController alloc] initWithNibName:#"ContactFormViewController" bundle:nil];
[form setContactType:ContactTypeCustomer];
form.posCustomer = customer;
[self.navigationController pushViewController:form animated:YES];
}
-(void)showEditSupplier:(POSSupplier *)supplier{
ContactFormViewController *form = [[ContactFormViewController alloc] initWithNibName:#"ContactFormViewController" bundle:nil];
[form setContactType:ContactTypeSuppplier];
form.posSupplier = supplier;
[self.navigationController pushViewController:form animated:YES];
}
#pragma mark
#pragma DBDelegate
- (void)requestDataCompleted:(NSMutableArray *)results
{
datasource = results;
[self.collectionView reloadData];
}
and here is for custom collectionview:
#synthesize displayname;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGRect frame = self.contentView.frame;
UIView *view = [[UIView alloc] initWithFrame:frame];
view.layer.borderWidth = 0.5;
[view.layer setBorderColor:[UIColor colorWithRed:0.3 green:0.6 blue:0.2 alpha:1].CGColor];
view.layer.cornerRadius = 5;
[view setBackgroundColor:[UIColor colorWithRed:0.3 green:0.6 blue:0.2 alpha:0.3]];
UIImageView *avatarView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, frame.size.width, rect.size.width)];
[avatarView setBackgroundColor:[UIColor clearColor]];
[avatarView setImage:[UIImage imageNamed:#"users_icon"]];
[view addSubview:avatarView];
UILabel *displayName = [[UILabel alloc] initWithFrame:CGRectMake(3, frame.size.width - 10, rect.size.width - 6, 50)];
displayName.numberOfLines = 2;
displayName.text = displayname;
[displayName setFont:[UIFont fontWithName:#"Arial" size:12]];
displayName.textAlignment = NSTextAlignmentCenter;
[displayName setTextColor:[UIColor colorWithRed:0.3 green:0.6 blue:0.2 alpha:1]];
[view addSubview:displayName];
[self.contentView addSubview:view];
}
Thanks for any helps.
Because of this line [self.contentView addSubview:view];, It've added multiple times because setNeedDisplay will call drawRect: every time. To avoid this, try below..
UIView *view = [[UIView alloc] initWithFrame:frame];
view.tag = SomeTagValue;
.
.
.
.
UIView *preView = [self.contentView viewWithTag:SomeTagValue];
[preView removeFromSuperview];
[self.contentView addSubview:view];
1)i want to create a striked text so i have used thet ext frame and i have created it and keep on the lbal it works fine when i have used it in the table cell when i am selecting the cell the strike on the label disaperared
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
#pragma mark TableView delegate method.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView1
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView1 numberOfRowsInSection:(NSInteger)section
{
return 4;
}
- (CGFloat)tableView:(UITableView *)tableView1 heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 120;
}
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
tableView1.backgroundColor = [UIColor clearColor];
cell=nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
//cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor clearColor];
// cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"strip_11C.png"]];
//cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"strip_11C_h.png"]];
UILabel *price1_lbl = [[UILabel alloc] initWithFrame:CGRectMake(2,80,100,26)];
price1_lbl.text = #"label";
price1_lbl.textColor = [UIColor grayColor];
price1_lbl.font = [UIFont fontWithName:#"Arial-BoldMT" size:(13.0)];
price1_lbl.backgroundColor = [UIColor clearColor];
NSString *string =price1_lbl.text;
CGSize stringSize = [string sizeWithFont:price1_lbl.font];
CGRect buttonFrame = price1_lbl.frame;
CGRect labelFrame = CGRectMake(buttonFrame.origin.x ,
4+buttonFrame.origin.y + stringSize.height/2,
stringSize.width, 1);
UILabel *lineLabel = [[UILabel alloc] initWithFrame:labelFrame];
lineLabel.backgroundColor = [UIColor blackColor];
[cell.contentView addSubview:price1_lbl];
[cell.contentView addSubview:lineLabel];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
secondviewcontroller *obj=[[secondviewcontroller alloc]initWithNibName:#"secondviewcontroller" bundle:nil];
[self.navigationController pushViewController:obj animated:YES];
}
Try to add the strikethrough line directly to the label with the text (price1_lbl). If that doesn't work you may want to give this lib a try.
UPDATE:
You can use this code:
CGSize size = [price1_lbl.text sizeWithFont:[UIFont systemFontOfSize:16.0f]];
UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0 , label.frame.size.height / 2, size.width, 1)];
line.backgroundColor = [UIColor blackColor];
[price1_lbl addSubview:line];