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];
Related
The source codeļ¼
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.tableView];
}
- (UITableView *)tableView
{
if (!_tableView) {
CGRect frame = self.view.bounds;
frame.size.height -= (64 + 40);
_tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
_tableView.separatorColor = __color_ListSepatator;
_tableView.dataSource = self;
_tableView.delegate = self;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.showsVerticalScrollIndicator = NO;
_tableView.showsHorizontalScrollIndicator = NO;
}
return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return 4;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 45.0f;
}
/*Here is dalegate*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *string_id = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string_id];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string_id];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = [NSString stringWithFormat:#"%ld",(long)indexPath.row];
/*Here is jammed*/
cell.accessoryView = self.viewccessory;
return cell;
}
/*Here is propety*/
- (UIView *)viewccessory
{
if (!_viewccessory) {
_viewccessory = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
_viewccessory.backgroundColor = [UIColor orangeColor];
}
return _viewccessory;
}
Question
When different cell repeat the same accessoryView Settings, getting stuck when I was out of the controller, can't into the interface
Each time when the controller card dead don't know why.Why can't accessoryView set the same view?Ask everybody to help. Thanks
accessoryView for each tableview cell should be independent but not shared, you should make a new view very time when assigning to table view cell.
Why acessoryView can't be shared? Each view should only has one superview.
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 want to show UITableviewCell from nib file. And also want to add dynamic label on it. But the label is override it self.
Please provide me any method where I can call UITableviewCell from nib and also add dynamic label.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UINib *nib = [UINib nibWithNibName:#"ItemCell" bundle:nil];
[[self tblView] registerNib:nib forCellReuseIdentifier:#"ItemCell"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 150.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ItemCell"];
for (int i=0; i<4; i++) {
UILabel *lbl=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 20.0*i, 150.0, 20.0)];
lbl.text=#"This is custom cell.";
[cell.contentView addSubview:lbl];
}
return cell;
}
Use this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *lbl;
ItemCell *cell = (ItemCell *)[tableView dequeueReusableCellWithIdentifier:#"ItemCell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ItemCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
lbl=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 150.0, 60.0)];
lbl.tag = indexpath.row;
[cell.contentView addSubview:lbl];
}
lbl = (UIlabel*) [cell.contentView viewWithTag:indexpath.row];
lbl.text=#"This is custom cell.";
return cell;
}
Try this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ItemCell"];
UILabel* label = (UILabel*)[cell.containtView viewWithTag:indexPath.row];
if(!label) {
UILabel *lbl=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 150.0, 60.0)];
lbl.text=#"This is custom cell.";
lbl.tag = indexPath.row
[cell.contentView addSubview:lbl];
}
label.text = #"your text";
return cell;
}
This is my functions for my table View.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.categories count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return #"test";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"%d", self.collections.count);
return [self.collections count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
switch ([indexPath section])
{
case 0:
self.myList = self.collections;
break;
}
cell.textLabel.text = self.collections[indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 48;
}
Actually, with this code, my table view show all the section and cells in the same view.
However, I would like a table view which show, at first time, a row with my title of section.
When i click on the row where there is my title of section, i would like to show the cells which are in the section. How can i do that?
Do i need 2 tableViews?
You should have to take two uitableview in that delegate will be same after that you should have to use this code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"%#",indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==Nil)
{
cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
if (isDeptSelected)
{
//[cell.imageView setImage:[UIImage imageNamed:#"Department.png"]];
cell.textLabel.text = [[arrDept_Detail valueForKey:#"dep_Name"]objectAtIndex:indexPath.row];
}
else if (isEmpSelected)
{
[cell.imageView setImage:[UIImage imageNamed:#"Emp_Images"]];
cell.textLabel.text = [[arrEmp_Detail valueForKey:#"emp_Name"]objectAtIndex:indexPath.row];
}
// cell.textLabel.text=[[arrDept_Detail objectAtIndex:indexPath.row]valueForKeyPath:#"dep_Name"];
[cell setBackgroundColor:[UIColor brownColor]];
[cell.textLabel setTextColor:[UIColor greenColor]];
[cell.textLabel setFont:[UIFont fontWithName:#"Georgia-Bold" size:16]];
cell.textLabel.highlightedTextColor=[UIColor purpleColor];
[self.view setBackgroundColor:[UIColor brownColor]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (isDeptSelected)
{
isDeptSelected=false;
isEmpSelected=true;
[btnDoneclick setHidden:NO];
[self.view addSubview:btnDoneclick];
EmpInDepTableView=[[UITableView alloc]initWithFrame:CGRectMake(160, 284,0, 0)];
[EmpInDepTableView setDelegate:self];
[EmpInDepTableView setDataSource:self];
[EmpInDepTableView setHidden:NO];
[EmpInDepTableView setBackgroundColor:[UIColor brownColor]];
EmpInDepTableView.layer.borderWidth=3.0f;
EmpInDepTableView.layer.cornerRadius=10.0f;
[self.view addSubview:EmpInDepTableView];
self.tabBarController.tabBar.userInteractionEnabled=NO;
UITableViewCell *cell=[Dept_TableView cellForRowAtIndexPath:indexPath];
DeptId=[Dep_Detail fetchDeptId_DeptName:cell.textLabel.text];
arrEmp_Detail = [[Emp_Detail fetchEmp_By_DeptId:DeptId]mutableCopy];
[UITableView animateWithDuration:0.6 animations:^
{
[EmpInDepTableView setFrame:CGRectMake(0, 64,320, 430)];
[btnDoneclick setFrame:CGRectMake(0,490,320,29)];
}completion:^(BOOL finished)
{
[EmpInDepTableView reloadData];
}];
}
else if (isEmpSelected)
{
}
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] init];
[view setBackgroundColor:[UIColor brownColor]];
return view;
}
-(IBAction)btnDoneclicked:(id)sender
{
[EmpInDepTableView reloadData];
isDeptSelected=true;
isEmpSelected=false;
[btnDoneclick setHidden:YES];
self.tabBarController.tabBar.userInteractionEnabled=YES;
[UITableView animateWithDuration:0.6 animations:^
{
[EmpInDepTableView setFrame:CGRectMake(160, 284, 0, 0)];
[btnDoneclick setFrame:CGRectMake(160, 284, 0, 0)];
[self.view setBackgroundColor:[UIColor brownColor]];
}completion:^(BOOL finished)
{
}];
}
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;
}