I insert a segmented in uitableview like this url image. If you see the url image, now i'm working ok everything data in uitable,
and...
When i scroll down table i wanna part 1: Image(in url) will scroll scroll to part 2: segmented will stop and if you still scroll uitable part 3: the data will be scroll (keep segmented on the top).
Have you any idea it. Thanks
I just test it, it is working!
Set your UITableView pain
UITableView has two section and each section has only one row.
When edit your section header, make sure only second section has header - second header will be your part2.
first section first row is your part1.
second section first row is your part3.
That's it : )
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
- (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];
}
cell.textLabel.text = #"test";
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
#try {
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
switch (section) {
case 0:
{
//nothing
}
break;
case 1:
{
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
lbl.text = #"second";
lbl.textColor = [UIColor blackColor];
[containerView addSubview:lbl];
}
break;
}
return containerView;
}
#catch (NSException *exception) {
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 1000.0;
}
Related
In my table view i am using custom cell
In each section for one row and each section contains header. The section header increase or decrease the height based dynamic text. In that situation, section are jerking when i am scrolling.
# pragma mark - UITableView Delegate methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return auctionResultAry.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (auctionResultAry.count == 0) {
cell = [self tableView:self.tableView EmptycellForRowAtIndexPath:indexPath];
}
else{
cell = [self tableView:self.tableView latestofferscellForRowAtIndexPath:indexPath];
self.tableView.scrollEnabled=YES;
}
return cell;
}
-(UITableViewCell *)tableView:(UITableView *)tableView latestofferscellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentifier=#"Cell";
subcell=(CTEAuctionBidNowTVCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if (subcell==nil)
{
subcell =(CTEAuctionBidNowTVCell *) [[[NSBundle mainBundle] loadNibNamed:#"CTEAuctionBidNowTVCell" owner:self options:nil]objectAtIndex:0];
}
subcell.event_auction_name.text=[NSString stringWithFormat:#"%#",[[auctionResultAry objectAtIndex:indexPath.section ]objectForKey:#"auction_title"]];
subcell.selectionStyle=UITableViewCellSelectionStyleNone;
return subcell;
}
i am writing code in section header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView;
headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 60)];
headerView.backgroundColor = [UIColor lightGrayColor];
UILabel *auctionIdLbl=[[UILabel alloc] initWithFrame:CGRectMake(headerView.frame.size.width-115, headerView.frame.origin.y+5, 110, 20)];
auctionIdLbl.textColor =[UIColor blackColor];
auctionIdLbl.textAlignment=NSTextAlignmentRight;
auctionIdLbl.font=[UIFont fontWithName:#"Arial-BoldMT" size:13];
auctionIdLbl.text = [NSString stringWithFormat:#"ID: %#",[[auctionResultAry objectAtIndex:section]objectForKey:#"auction_id"]];
[headerView addSubview:auctionIdLbl];
UILabel *titleLnbl = [[UILabel alloc]initWithFrame:CGRectMake(5, headerView.frame.origin.y+5, +headerView.frame.size.width-100, 150)];
titleLnbl.textColor =[UIColor blackColor];
titleLnbl.font=[UIFont fontWithName:#"Arial-BoldMT" size:13];
titleLnbl.numberOfLines=0;
titleLnbl.textAlignment=NSTextAlignmentLeft;
titleLnbl.text = [NSString stringWithFormat:#"%#",[[auctionResultAry objectAtIndex:section]objectForKey:#"auction_title"]];
CGSize size = [self getSizebasedOnText:titleLnbl];
CGFloat titleLnblHght=size.height;
CGRect updateLblHght= titleLnbl.frame;
updateLblHght.size.height=titleLnblHght;
titleLnbl.frame=updateLblHght;
[headerView addSubview:titleLnbl];
return headerView;
}
its due to change in height of row in uitableview.If row height is big jerking is not frequent.
I an creating app that needs expand tableview feature like below screen. when i click the row the textbox and button will appear.when i click on textbox and press return from keyboard, application crashed.
When I tried to resign the text box showing in screenshot, application crashed,
I write Following Code...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"AffiliationCell"];
cell.backgroundColor = [UIColor clearColor];
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 130, 44)];
lblTitle.tag = indexPath.row+1;
lblTitle.font = [UIFont fontWithName:Oxygen_Regular size:13];
lblTitle.text = [NSString stringWithFormat:#"Affiliation %d",indexPath.row+1];
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview:lblTitle];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* checkCell = [tableView cellForRowAtIndexPath:indexPath];
[tableView beginUpdates];
if ([indexPath compare:self.checkedIndexPath] == NSOrderedSame) {
self.checkedIndexPath = nil;
[viewScreenName removeFromSuperview];
} else {
//add view
[txtScreenName setClearsOnInsertion:YES];
viewScreenName.frame = CGRectMake(0, 45, viewScreenName.frame.size.width, viewScreenName.frame.size.height);
[checkCell.contentView addSubview:viewScreenName];
self.checkedIndexPath = indexPath;
}
[tableView endUpdates];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if ([indexPath compare:self.checkedIndexPath] == NSOrderedSame) {
return expandedCellHeight; // Expanded height
}
return 44 ;
}
#pragma mark - TextField Delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
tblAffiliations.frame = updatedFrame;
return TRUE;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
#try {
if (textField) {
[textField resignFirstResponder];
}
}
#catch (NSException *exception) {
NSLog(#"Exception %#",exception);
}
tblAffiliations.frame = currentFrame;
return TRUE;
}
Please Help.
It seems that you are using a textfield that is being deallocated. I think the best way to proceed is adding the textfield in each cell like you added your label and setting it to be hidden. On the didSelectRow delegate, you should set the label as hidden and the textfield not hidden. It is better to work with hidden flag that to remove and add from superview.
Wish it helps.
I've added a UITableViewController to my main view controller using method from Apple docs:
- (void)viewWillAppear:(BOOL)animated {
myTableViewController = [MyTableViewController new];
[self displayContentController:myTableViewController];
}
- (void)displayContentController:(UIViewController *)viewController; {
[self addChildViewController:viewController];
viewController.view.frame = self.view.bounds;
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
}
and I wanted to display some example cells in this controller:
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
(I don't think I need any more configuration as it is UITableViewController, not UITableView)
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.textLabel.text = #"Under development";
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
But all I can see is just empty cells (I've also tried to change background color to make sure it's not just the while label color):
After few hours I have no idea what I did wrong. Do you have any idea what am I missing?
You have to used it:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"cell"];
if(!cell)
{
cell=[[UITableViewCell alloc]init];
}
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.text = #"Under development";
return cell;
}
Try it:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return Table_Arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"subwaysurfer"];
if(!cell)
{
cell=[[UITableViewCell alloc]init];
}
UIImageView *imgg;
UILabel *txt;
UIImageView *Acc_img;
imgg=[[UIImageView alloc]initWithFrame:CGRectMake(10,5, 50, 50)];
imgg.image = [UIImage imageNamed:[imageArr objectAtIndex:indexPath.row]];
cell.highlighted=YES;
cell.backgroundColor=[UIColor clearColor];
txt=[[UILabel alloc]initWithFrame:CGRectMake(20, 0, self.view.frame.size.width-80, 60)];
txt.numberOfLines = 3;
txt.text=[Table_Arr objectAtIndex:indexPath.row];
txt.backgroundColor=[UIColor clearColor];
// txt.textColor=[UIColor colorWithRed:66.0/255.0 green:51.0/255.0 blue:57.0/255.0 alpha:1.0];
txt.textColor=[UIColor colorWithRed:44.f/255.f green:78.f/255.f blue:134.f/255.f alpha:1];
txt.font=[UIFont fontWithName:#"AvenirNextCondensed-Medium" size:16.0];
Acc_img=[[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width-40, 20, 20, 20)];
Acc_img.image=[UIImage imageNamed:#"arrow.png"];
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad){
imgg.frame = CGRectMake(10, 5, 80, 80);
txt.frame = CGRectMake(120, 10, self.view.frame.size.width-160, 70);
Acc_img.frame = CGRectMake(self.view.frame.size.width-80, 25, 40, 40);
txt.font=[UIFont systemFontOfSize:28.0];
}
[cell addSubview:imgg];
[cell addSubview:txt];
[cell.contentView addSubview:Acc_img];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
// fixed font style. use custom view (UILabel) if you want something different
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad){
return 90;
}
else{
return 60;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
return [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0)];
}
Ok, problem solved. It appears, that even using UITableViewController which has built-in property tableView it is necessary to initialise the tableView (or at least it did help me).
self.tableView = [UITableView new];
In my app, I have created a mutable array named "array".
array=[[NSMutableArray alloc]initWithObjects:#"a",#"b",#"c",#"d",#"e", nil];
My tableView contains only one row for multiple section, Each section has a customView which contains only one label.
My custom View name is ContentOfCell
ContentOfCell * contentOfCell=[[ContentOfCell alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
[cell.contentView addSubview:contentOfCell];
I have added the array to the label
contentOfCell.label.text=[array objectAtIndex:indexPath.section];
My problem is that it recognises only the first 4 values in the array and the first 4 values is getting repeated in the sections
I thing something is going wrong here
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [indexPath row]+50;
}
If I change 50 by 100, the error occurs if not the values are inserted correctly
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * cellIdentifier=[NSString stringWithFormat:#"MyTableView"];
cell=[myTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell== nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
ContentOfCell * contentOfCell=[[ContentOfCell alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
[cell.contentView addSubview:contentOfCell];
contentOfCell.nameField.text=[arr objectAtIndex:indexPath.section];
cell.contentView.backgroundColor=[UIColor whiteColor];
}}
Here the view contains only 3 rows, when I scroll down the array is initiated again, then the values are printed again
I guess you are not reusing the cell correctly.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * cellIdentifier=[NSString stringWithFormat:#"MyTableView"];
cell=[myTableView dequeueReusableCellWithIdentifier:cellIdentifier];
ContentOfCell *contentOfCell;
//Initialization Part. declare objects here
if(cell== nil)
{
//Creation part. Create your new cell here. Do all UI actions here
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
contentOfCell=[[ContentOfCell alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
contentOfCell.tag =100;
[cell.contentView addSubview:contentOfCell];
cell.contentView.backgroundColor=[UIColor whiteColor];
}
else{
// Reusable part. Reuse the UI controls here from existing cell
contentOfCell = (ContentOfCell *)[cell.contentView viewWithTag:100];
}
//Assign all the data here
contentOfCell.nameField.text=[arr objectAtIndex:indexPath.section];
}
This is one of the simple mistake often committed by iOS Rookies.
Kindly check the Apple developer documentation here to understand how a tableview cell is reused
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html
I implemented same code as below with grouped table.Everything is fine.Please compare your code with below code.
- (void)viewDidLoad
{
[super viewDidLoad];
arrData=[[NSMutableArray alloc] initWithObjects:#"a",#"b",#"c",#"d",#"e",#"f",#"g",#"h",#"i",#"j",#"k", nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrData count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return 50.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *aCell=[tableView dequeueReusableCellWithIdentifier:#"da"];
if(aCell==Nil)
{
aCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"da"];
ContentOfCell *ContentOfCellObj=[[ContentOfCell alloc] initWithFrame:CGRectMake(0,0,320,50)];
ContentOfCellObj.tag=101;
[aCell.contentView addSubview:ContentOfCellObj];
}
ContentOfCell *ContentOfCellObj=(ContentOfCell*)[aCell.contentView viewWithTag:101];
ContentOfCellObj.nameField.text=[NSString stringWithFormat:#"%#",[arrData objectAtIndex:indexPath.section]];
return aCell;
}
Use this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellIdentifier=#"CellIdentifier";//or your custom name
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell== nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
ContentOfCell * contentOfCell=[[ContentOfCell alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
contentOfCell.tag = 101;
[cell.contentView addSubview:contentOfCell];
cell.contentView.backgroundColor=[UIColor whiteColor];
}
ContentOfCell * contentOfCell = (ContentOfCell*)[cell.contentView viewWithTag:101];
contentOfCell.nameField.text=[arr objectAtIndex:indexPath.section];
return cell;
}
Make sure you set
myTableView.dataSource = self;
and these two delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [arrData count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
Try this code, surely it will resolve your problem.
Step 1. First declare array as global variable
#interface YOUR-CLASS-NAME ()
{
NSMutableArray *array
}
Step 2. Now set no of sections you need. This means how many time do you want to repeat the array values in table continuously.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
Step 3. Now set no of rows you need to display from your array. This means no of values from your array need to display
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
UITableView showing separator even there is no rows. But when I use viewForFooterInSection the separator is gone. I want it to keep showing up. What should I do ?
Here is my code when I add footer in it :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"s"];
cell.textLabel.text = [NSString stringWithFormat:#"%d", indexPath.row + 1];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
return cell;
}
// -------------------------- footer code start here --------------------------
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
view.backgroundColor = [UIColor grayColor];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
title.text = #"footer here";
title.textAlignment = NSTextAlignmentCenter;
title.textColor = [UIColor whiteColor];
[view addSubview:title];
return view;
}
this github project may give you some advices:
CLTableWithFooterViewController
Of course need some change if you would like the footer always display on the screen even if with a lot of cells.
remove - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
and - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
use this code to keep the footer down (but always visible) and keep the separator:
#implementation tableView {
__weak UIView *_staticView;
}
- (void)viewDidLoad {
UIView *staticView = [[UIView alloc] initWithFrame:CGRectMake(0, self.tableView.bounds.size.height-50, self.tableView.bounds.size.width, 50)];
staticView.backgroundColor = [UIColor redColor];
//add other code for UILabel here
[self.tableView addSubview:staticView];
_staticView = staticView;
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
_staticView.transform = CGAffineTransformMakeTranslation(0, scrollView.contentOffset.y);
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// this is needed to prevent cells from being displayed above our static view
[self.tableView bringSubviewToFront:_staticView];
}
I guess you just have a single section,right? Before you using viewForFooterInSection,it just fills blank cells all the way down.But when you call viewForFooterInSection,tableview will put a footer at the end of the section.Since you don't have the second section,it stops draw any cells.So you can make a blank second section,it will fill blank cells to the second section.