Add rows and columns in UITableView iOS - ios

Is it possible to add rows and columns to an UITableview in iPhone as shown below?
Please help me, I'm new to iOS development.

I found the answer.
UIView *newView124=[[UIView alloc]initWithFrame:CGRectMake(224, 90, 2, 48)];
newView124.backgroundColor=[UIColor grayColor];
[cell addSubview:newView124];
//Initialize Label
NSLog(#"cell width: %f",cell.frame.size.width);
UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 90, 100, 50)];
[lbl1 setFont:[UIFont fontWithName:#"FontName" size:12.0]];
[lbl1 setTextColor:[UIColor blackColor]];
lbl1.text = #"1";
[cell addSubview:lbl1];
UIView *newView=[[UIView alloc]initWithFrame:CGRectMake(40, 90, 2, 48)];
newView.backgroundColor=[UIColor grayColor];
[cell addSubview:newView];
UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(42, 90, 130, 50)];
[lbl2 setFont:[UIFont fontWithName:#"FontName" size:12.0]];
[lbl2 setTextColor:[UIColor blackColor]];
lbl2.text = #"Hospitsal";
[cell addSubview:lbl2];
UIView *newView1=[[UIView alloc]initWithFrame:CGRectMake(172, 90, 2, 48)];
newView1.backgroundColor=[UIColor grayColor];
[cell addSubview:newView1];
UILabel *lbl21 = [[UILabel alloc]initWithFrame:CGRectMake(174, 90, 50, 50)];
[lbl21 setFont:[UIFont fontWithName:#"FontName" size:12.0]];
[lbl21 setTextColor:[UIColor blackColor]];
lbl21.text = #"Active";
[cell addSubview:lbl21];
UIView *newView12=[[UIView alloc]initWithFrame:CGRectMake(224, 90, 2, 48)];
newView12.backgroundColor=[UIColor grayColor];
[cell addSubview:newView12];
UIButton *modeButton = [[UIButton alloc] initWithFrame:CGRectMake(250, 100, 30, 25)];
UIImage *ddd=[UIImage imageNamed:#"pencil.png"];
[modeButton setImage:ddd forState:UIControlStateNormal];
///[btnEdit addTarget:self action:#selector(imSelected) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:modeButton];
// [cell.contentView bringSubviewToFront:btnEdit];
UIView *aView1=[[UIView alloc]initWithFrame:CGRectMake(0, 135,self.view.frame.size.width, 2)];
aView1.backgroundColor=[UIColor grayColor];
[cell addSubview:aView1];

Related

mulpitle UILabel in Navigation titleView [duplicate]

I am developing one application for iphone and in that i have some issues regarding adding more then one UILabel in navigation bar at a specific position.
What i want is in following images
in above image there is one backbar button and one imageview as shown with arrow-mark. Other then that all white box is for difrent UILabels to show in navigation bar.
There is only one UIImageView in the navigation bar and one left bar button. Now problem is that i don't know how to add multiple UILabel in navigation bar at a specific position.
Till now what i have worked is to add only button and UIImageView with the right bar button array or left bar button array, but that only add items in sequnce.
So can anyone please guide me that how can anyone add UILabels or any other item at a specific position..
you can add Multiple UIlabel or Image as look like bellow image:-
this can do using bellow code you can change Label and imageView frame and put as your requirement
- (void)viewDidLoad
{
UIView *btn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake(5, 25, 200, 16)];
label.tag = 1;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor whiteColor];
label.text = #"My first lable";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 200, 16)];
label.tag = 2;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor whiteColor];
label.text = #"second line";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];
UIImageView *imgviw=[[UIImageView alloc]initWithFrame:CGRectMake(170, 10, 20, 20)];
imgviw.backgroundColor = [UIColor blackColor];
imgviw.image=[UIImage imageNamed:#"a59117c2eb511d911cbf62cf97a34d56.png"];
[btn addSubview:imgviw];
self.navigationItem.titleView = btn;
}
You can add subviews directly to the navigationBar -
UINavigationBar *navBar = navController.navigationBar;
[navBar addSubview: yourLabel];
yourLabel frame origin will be relative to the navigation bar
Try this code
UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
buttonContainer.backgroundColor = [UIColor clearColor];
UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
[button0 setFrame: CGRectMake(160, 7 , 40, 30)];
[button0 setTitle:#"Sort" forState:UIControlStateNormal];
button0.titleLabel.font = [UIFont fontWithName:#"Helvetica" size:12];
[button0 setBackgroundImage:[UIImage imageNamed:#"Btn_Sort_Btn_Sort_Places.png"] forState:UIControlStateNormal];
[button0 addTarget:self action:#selector(sortAction) forControlEvents:UIControlEventTouchUpInside];
[button0 setShowsTouchWhenHighlighted:YES];
[buttonContainer addSubview:button0];
self.navigationItem.titleView = buttonContainer;
UILabel *lbl_title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0 , 140, 40)];
lbl_title.text = str_HeaderTitle;
lbl_title.textColor = [UIColor whiteColor];
lbl_title.font = [UIFont fontWithName:#"Helvetica-Bold" size:16];
lbl_title.backgroundColor = [UIColor clearColor];
lbl_title.textAlignment = NSTextAlignmentCenter;
[buttonContainer addSubview:lbl_title];
You can set an arbitrary view instead of a view controller's title:
myViewController.navigationItem.titleView = someView;
Note that most likely the view's frame will be restricted to make room for leftBarButtonItem and rightBarButtonItem.
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(50, 2, 20, 15)];
[label setBackgroundColor:[UIColor greenColor]];
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(40, 20, 150, 10)];
[imgView setBackgroundColor:[UIColor redColor]];
[[self.navigationController navigationBar] addSubview:label];
[self.navigationController.navigationBar addSubview:imgView];
Have a look at this piece of code. With some modifications it should solve your problem:
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[imageButton setFrame:CGRectMake(15, 0, 57, 44)];
[imageButton setBackgroundImage:[UIImage imageNamed:#"someImage.png"] forState:UIControlStateNormal];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 0, 250, 44)];
[titleLabel setText:#"some title"];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[self.navigationController.navigationBar addSubview:imageButton];
[self.navigationController.navigationBar addSubview:titleLabel];
Just modify the values in the CGRectMake() to fit your needs.

Customize the UIButton resize according to text length and place UILabel after that

I have used following code and got error as below i.e less space in small text and more space long text
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
UIButton *BtnBreadcrumb = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[BtnBreadcrumb addTarget:self action:#selector(selectBtnBreadcrumb:)
forControlEvents:UIControlEventTouchUpInside];
[BtnBreadcrumb setTitle:selectedDepartment forState:UIControlStateNormal];
BtnBreadcrumb.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
BtnBreadcrumb.tintColor=ThemeColor;
CGSize stringsize = [selectedDepartment sizeWithAttributes:#{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]}];
BtnBreadcrumb.frame = CGRectMake(10, 5, stringsize.width, 40);
[view addSubview:BtnBreadcrumb];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(stringsize.width, 5, 200, 40)];
//[label setFont:[UIFont boldSystemFontOfSize:12]];
NSString *string = [NSString stringWithFormat:#"/ %#",selectedCategory];
label.font = [UIFont fontWithName:#"Helvetica" size:15.0];
label.textColor = TextColor;
[label setText:string];
[view addSubview:label];
[view setBackgroundColor:[UIColor colorWithRed:0.933f green:0.933f blue:0.933f alpha:1.00f]];
return view;
}
I have study following links
iOS: UIButton resize according to text length
How can i increase the button width dynamically depends on the text size in iphone?
Replacement for deprecated -sizeWithFont:constrainedToSize:lineBreakMode: in iOS 7?
Replacement for deprecated sizeWithFont: in iOS 7?
IOS 7 sizeWithFont Deprecated
Please use:
CGSize size1 = [strkeyword1Partner1 sizeWithAttributes:#{NSFontAttributeName:[UIFont fontWithName:FONT_MYriad_REGULAR size:14.0f]}];
// Values are fractional -- you should take the ceilf to get equivalent values
CGSize adjustedSize1= CGSizeMake(ceilf(size1.width), ceilf(size1.height));
[btnKewword1 setFrame:CGRectMake(self.view.frame.size.width-(adjustedSize1.width +14), btnKewword.frame.origin.y, adjustedSize1.width+5, 20)];
Try this
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(50, 100, 100, 0);
btn.titleLabel.numberOfLines = 0;
[btn setTitle:#"dsgdfgdfsdsfgdfsgdfsgdfsgdfgdfgfkdsfgdsgdksfgkdskgf" forState:UIControlStateNormal];
[self.view addSubview:btn];
Please try following code I add the size of the UIButton.
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
UIButton *BtnBreadcrumb = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[BtnBreadcrumb addTarget:self action:#selector(selectBtnBreadcrumb:)
forControlEvents:UIControlEventTouchUpInside];
[BtnBreadcrumb.titleLabel setFont: [BtnBreadcrumb.titleLabel.font fontWithSize:17]];
[BtnBreadcrumb setTitle:#"Test" forState:UIControlStateNormal];
BtnBreadcrumb.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
BtnBreadcrumb.tintColor=ThemeColor;
CGSize stringsize = [selectedDepartment sizeWithAttributes:#{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]}];
BtnBreadcrumb.frame = CGRectMake(10, 5, stringsize.width, 40);
[view addSubview:BtnBreadcrumb];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(stringsize.width, 5, 200, 40)];
//[label setFont:[UIFont boldSystemFontOfSize:12]];
NSString *string = [NSString stringWithFormat:#"/ %#",selectedCategory];
label.font = [UIFont fontWithName:#"Helvetica" size:15.0];
label.textColor = TextColor;
[label setText:string];
[view addSubview:label];
[view setBackgroundColor:[UIColor colorWithRed:0.933f green:0.933f blue:0.933f alpha:1.00f]];
return view;
}

custom UITableViewCell doesn't show data before showing full cell when scrolling

My application has custom UITableViewCell with large height 470px.
I created custom cell with fully programatically.
This is my cellForRowAtIndexPath method
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
pixDealsTableViewCell *dealCell = (pixDealsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"Deals" forIndexPath:indexPath];
if (dealCell == nil) {
dealCell = [[pixDealsTableViewCell alloc] init];
}
dealCell.dBusinessLogo.image = [pixUtil imageWithImage:[UIImage imageNamed:#"profile_icon"] scaledToSize:CGSizeMake(60, 60)];
[dealCell.dBusinessName setTitle:#"Business Name" forState:UIControlStateNormal];
dealCell.dTimeAgo.text = [NSString stringWithFormat:#"5 min ago"];
[dealCell.dHeading setTitle:#"Deals Head" forState:UIControlStateNormal];
[dealCell.dDescription setTitle:#"Descriptionskjdfnkjsdnfis" forState:UIControlStateNormal];
dealCell.dImage.image = [pixUtil imageWithImage:[UIImage imageNamed:#"carrier.png"] scaledToSize:CGSizeMake(290, 250)];
[dealCell.dLike setTitle:#"Like" forState:UIControlStateNormal];
[dealCell.dShare setTitle:#"Share" forState:UIControlStateNormal];
return dealCell;
}
This is my custom cell init method
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
NSLog(#"NEW CELL");
customRed = [[UIColor alloc]initWithRed:209.0f/255.0f green:19.0f/255.0f blue:38.0f/255.0f alpha:1.0f];
[self setBackgroundColor:[UIColor grayColor]];
containerCell = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 300, 700)];
[containerCell setBackgroundColor:[UIColor whiteColor]];
self.dBusinessLogo = [[UIImageView alloc]initWithFrame:CGRectMake(15, 15, 60, 60)];
self.dBusinessName = [[UIButton alloc]initWithFrame:CGRectMake(80, 20, 225, 20)];
[self.dBusinessName setTitle:#"Business Name" forState:UIControlStateNormal];
self.dBusinessName.titleLabel.font = [UIFont boldSystemFontOfSize:14];
[self.dBusinessName addTarget:self action:#selector(haveAccount:) forControlEvents:UIControlEventTouchUpInside];
[self.dBusinessName setTitleColor:customRed forState:UIControlStateNormal];
self.dTimeAgo = [[UILabel alloc]initWithFrame:CGRectMake(80, 50, 225, 15)];
self.dHeading = [[UIButton alloc]initWithFrame:CGRectMake(15, 80, 290, 20)];
self.dHeading.titleLabel.font = [UIFont boldSystemFontOfSize:14];
[self.dHeading addTarget:self action:#selector(haveAccount:) forControlEvents:UIControlEventTouchUpInside];
[self.dHeading setTitleColor:customRed forState:UIControlStateNormal];
self.dDescription = [[UIButton alloc]initWithFrame:CGRectMake(15, 105, 290, 50)];
self.dDescription.titleLabel.font = [UIFont boldSystemFontOfSize:14];
[self.dDescription addTarget:self action:#selector(haveAccount:) forControlEvents:UIControlEventTouchUpInside];
[self.dDescription setTitleColor:customRed forState:UIControlStateNormal];
self.dImage = [[UIImageView alloc]initWithFrame:CGRectMake(15, 160, 290, 250)];
self.dLike = [[UIButton alloc]initWithFrame:CGRectMake(10, 420, 150, 40)];
self.dLike.titleLabel.font = [UIFont boldSystemFontOfSize:14];
[self.dLike addTarget:self action:#selector(haveAccount:) forControlEvents:UIControlEventTouchUpInside];
[self.dLike setTitleColor:customRed forState:UIControlStateNormal];
self.dShare = [[UIButton alloc]initWithFrame:CGRectMake(160, 420, 150, 40)];
self.dShare.titleLabel.font = [UIFont boldSystemFontOfSize:14];
[self.dShare addTarget:self action:#selector(haveAccount:) forControlEvents:UIControlEventTouchUpInside];
[self.dShare setTitleColor:customRed forState:UIControlStateNormal];
[self.contentView addSubview:containerCell];
[self.contentView addSubview:self.dBusinessLogo];
[self.contentView addSubview:self.dBusinessName];
[self.contentView addSubview:self.dTimeAgo];
[self.contentView addSubview:self.dHeading];
[self.contentView addSubview:self.dDescription];
[self.contentView addSubview:self.dImage];
[self.contentView addSubview:self.dShare];
// NSLog(#"Deals View added");
}
return self;
}
This is my UITableViewCell fully viewed.
When I scroll down to next cell, It doesn't load properly.
It loads correctly after fully scrolled the cell to up
Please Help me to solve this problem.
Finally I got answer. I created containerCell view with height of 700. But cell height is 470 only.
I changed that containerCell view height to 470. Its fixed my problem. :)

Avoid my section header to be redraw in viewForHeaderInSection

I have a UITableView with only 1 section title that contains labels when i reload data some of the label of the section may be hidden. But when I do reload data they are redraw in section I just wanted to know if there is some sort of dequeueReusableCellWithIdentifier: to avoid this like for the cell but for secions.
Here is the code
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 36)];
/* Create custom view to display section header... */
UILabel *labelSeq = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, 30, 18)];
[labelSeq setFont:[UIFont boldSystemFontOfSize:12]];
labelSeq.text = #"SEQ";
UILabel *labelSup = [[UILabel alloc] initWithFrame:CGRectMake(50, 2, 330, 18)];
[labelSup setFont:[UIFont boldSystemFontOfSize:12]];
labelSup.text = #"SUPPLIER";
UILabel *labelMod = [[UILabel alloc] initWithFrame:CGRectMake(390, 2, 150, 18)];
[labelMod setFont:[UIFont boldSystemFontOfSize:12]];
labelMod.text = #"MODEL";
UILabel *labelFinish = [[UILabel alloc] initWithFrame:CGRectMake(550, 2, 150, 18)];
[labelFinish setFont:[UIFont boldSystemFontOfSize:12]];
labelFinish.text = #"FINISH";
UILabel *labelQty = [[UILabel alloc] initWithFrame:CGRectMake(710, 2, 30, 18)];
[labelQty setFont:[UIFont boldSystemFontOfSize:12]];
labelQty.text = #"QTY";
UILabel *labelSale = [[UILabel alloc] initWithFrame:CGRectMake(750, 2, 125, 18)];
[labelSale setFont:[UIFont boldSystemFontOfSize:12]];
labelSale.textAlignment = UITextAlignmentRight;
labelSale.text = #"SALE PRICE";
labelSale.tag = 100;
UILabel *labelCost = [[UILabel alloc] initWithFrame:CGRectMake(885, 2, 125, 18)];
[labelCost setFont:[UIFont boldSystemFontOfSize:12]];
labelCost.textAlignment = UITextAlignmentRight;
labelCost.text = #"COST";
labelCost.tag = 200;
UILabel *labelDesc = [[UILabel alloc] initWithFrame:CGRectMake(50, 17, 700, 18)];
[labelDesc setFont:[UIFont boldSystemFontOfSize:12]];
labelDesc.text = #"DESCRIPTION";
UILabel *labelUnit = [[UILabel alloc] initWithFrame:CGRectMake(750, 17, 125, 18)];
[labelUnit setFont:[UIFont boldSystemFontOfSize:12]];
labelUnit.textAlignment = UITextAlignmentRight;
labelUnit.text = #"UNIT PRICE";
labelUnit.tag = 300;
UILabel *labelProfit = [[UILabel alloc] initWithFrame:CGRectMake(885, 17, 125, 18)];
[labelProfit setFont:[UIFont boldSystemFontOfSize:12]];
labelProfit.textAlignment = UITextAlignmentRight;
labelProfit.text = #"PROFIT";
labelUnit.tag = 400;
[view addSubview:labelSeq];
[view addSubview:labelSup];
[view addSubview:labelMod];
[view addSubview:labelFinish];
[view addSubview:labelQty];
[view addSubview:labelSale];
[view addSubview:labelCost];
[view addSubview:labelDesc];
[view addSubview:labelUnit];
[view addSubview:labelProfit];
[view setBackgroundColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0]]; //your background color...
_headerView = view;
return view;
}
Add a property to your class to hold on to that UIView. Then in -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section, just check to see if that property is nil or not. If it's nil, create it and set that property to it, otherwise just use the UIView saved in the property. If it requires updating it some, then you can do that at that point. It looks like all your content is static, so you should never need to update the view at all after the first time you create it.

How to Center Text in UITableView.tableHeaderView?

I am adding a UILabel to the tableHeaderView but for some reason it is left aligned.
self.messageLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self.messageLabel setText:#"Nothing to display!"];
[self.tableView setTableHeaderView:self.messageLabel];
[self.tableView.tableHeaderView setBackgroundColor:[UIColor blueColor]];
[self.tableView.tableHeaderView setFrame:CGRectMake(320/2, 480/2, 100, 20)];
[self.messageLabel setCenter:self.tableView.tableHeaderView.center];
[label setTextAlignment:UITextAlignmentCenter];

Resources