I want show a tableView into a cell like:
_____________________________________
| (IMAGE) ______________ | --CELL 0
| | MyCustomText|--CELL 0.0 |
| |_____________| |
| |MyCustomText2|--CELL 0.1 |
| |_____________| |
| |
| |
--------------------------------------
_____________________________________
| (IMAGE) 2 | --CELL 1
--------------------------------------
I'm trying add a tableView in storyboard and then connect with a new TableviewController and with a new CustomCellTableView, but this not show anything in the row's table.
It's possible? How can I declare the viewController or needn't add ViewController?
Thanks!
I think you don't need a new tableview controller. In your table cell, you can add another tableview.
#interface ImageCell : UITableViewCell<UITableViewDataSource,UITableViewDelegate>
#property (retain, nonatomic) IBOutlet UITableView *abc;
#end
and implement datasource and delegate method as usual.
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<##"reuseIdentifier"#> forIndexPath:indexPath];
// Configure the cell...
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView == self.tableView) {
return 8;
} else {
return 3;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (tableView == self.tableView) {
cell = [self.tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
if (indexPath.row == 0) {
self.childTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 100)];
self.childTableView.delegate = self;
self.childTableView.dataSource = self;
[cell.contentView addSubview:self.childTableView];
}else {
cell.textLabel.text = #"Pavan";
}
} else if (tableView == self.childTableView){
cell = [self.tableView dequeueReusableCellWithIdentifier:#"childCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"childCell"];
}
cell.textLabel.text = #"hi";
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.tableView) {
if (indexPath.row == 0) {
return 250;
} else {
return 100;
}
} else {
return 50;
}
}
Related
Lets say I have a tableview created in the storyboard and referenced as "tableViewJobList" and I want to generate a tableview in each cell of tableViewJobList so I do it in cellForRowAtIndexPath.
My .h file (tableViewJobList delegate and datasource is set to viewcontroller in storyboard)
#interface JobByAccountViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tableViewJobList;
...
#end
My .m file
...
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//realJoblist is my data array
if (tableView == _tableViewJobList) {
return [realJobList count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == _tableViewJobList) {
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == _tableViewJobList) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
//each cell has a tableview
UITableView *subTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
subTableView.delegate = self;
subTableView.dataSource = self;
subTableView.frame = CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height);
[cell addSubview:subTableView];
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == _tableViewJobList) {
NSLog(#"outer tableview");
} else {
NSLog(#"inner tableview");
}
...
}
...
In heightForRowAtIndexPath shouldn't the print statement trigger in the else block because those are the tableviews I created programmatically?
Try this,,,
you just replace your table outlet name.
- (void)viewDidLoad {
[super viewDidLoad];
tableViewJobList.delegate = self;
tableViewJobList.dataSource = self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//realJoblist is my data array
if (tableView == _tableViewJobList) {
return [realJobList count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == _tableViewJobList) {
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == _tableViewJobList) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
//each cell has a tableview
UITableView *subTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
tableViewJobList.delegate = self;
tableViewJobList.dataSource = self;
subTableView.frame = CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height);
[cell addSubview:subTableView];
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == _tableViewJobList) {
NSLog(#"outer tableview");
} else {
NSLog(#"inner tableview");
}
...
}
... `enter code here`
I have two table views in one viewController and a menu button.
Initially only tableView1 is displayed, when I press menu button the second table view should appear and the tableView1 is still there.
I was reading about and I implemented what I found, but without results.
Both table view appears, but data are the same and I don't want this. I was trying to do like this :
- (void) viewDidLoad {
tableView.hidden = NO;
tableViewMenu.hidden = YES;
tableView.delegate = self;
tableView.dataSource = self;
tableViewMenu.delegate = self;
tableViewMenu.dataSource = self;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == tableView) {
return 30;
}
else {
return 4;
}
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == tableView) {
static NSString *CellIdentifier1 = #"Cell1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] ;
}
cell.textLabel.text = #"Table 1";
NSLog(#"1here is%i %#",indexPath.row,cell.textLabel.text);
return cell;
} else {
static NSString *CellIdentifier2 = #"Cell2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] ;
}
cell.textLabel.text = #"Table 2";
return cell;
}
}
int i = 1;
- (void) showMenu {
//slide the content view to the right to reveal the menu
//tableView.hidden = NO;
tableViewMenu.hidden = NO;
NSLog(#"Display");
if (i == 1) {
[UITableView animateWithDuration:.25
animations:^{
[tableView setFrame:CGRectMake(tableViewMenu.frame.size.width, tableView.frame.origin.y, tableView.frame.size.width, tableView.frame.size.height)];
[testView setFrame:CGRectMake(tableViewMenu.frame.size.width, testView.frame.origin.y, testView.frame.size.width, testView.frame.size.height)];
}];
i = 0;
} else {
[UIView animateWithDuration:.25
animations:^{
[tableView setFrame:CGRectMake(0, tableView.frame.origin.y, tableView.frame.size.width, tableView.frame.size.height)];
[testView setFrame:CGRectMake(0, testView.frame.origin.y, testView.frame.size.width, testView.frame.size.height)];
tableViewMenu.hidden=YES;
}];
i = 1;
}
}
I guess the problem is the line :
if(tableView==tableView)
You should compare the tableview arguments with your #property.
If both the arguments and the iVar have the same name, the argument will override your iVar in the method scope.
if (tableview == self.tableView)
I set for tableview a tag and i did this :
if (tableView.tag==0) {
return 30;
}
else { return 4;
}
}
Idem is for cellForRowAtIndexPath
You have to have 2 table views:
#property (nonatomic, weak) IBOutlet UITableView* firstTableView;
#property (nonatomic, weak) IBOutlet UITableView* secondTableView;
When you press menu button you should change the delegate like this:
self.firstTableView.hidden = FALSE;
self.secondTableView.hidden = TRUE;
[self.firstTableView reloadData]
self.firstTableView.dataSource = self;
self.firstTableView.delegate = self;
or
self.firstTableView.hidden = TRUE;
self.secondTableView.hidden = FALSE;
[self.secondTableView reloadData]
self.secondTableView.dataSource = self;
self.secondTableView.delegate = self;
And in TableView Data Source:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.firstTableView) {
...
}
} else if (tableView == self.secondTableView){
...
}
And
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.firstTableView) {
return 30;
} else if (tableView == self.secondTableView){
return 4;
}
Can I achieve this?
if (indexpath.section == 0) {
// Use Class 1
} else if (indexpath.section == 1) {
// Use Class 2
}
I tried this but not working
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
OneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"One" forIndexPath:indexPath];
if( cell == nil){
cell = [[OneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"One"];
}
cell.oneLabel.text = #"HAHAHA";
return cell;
}else{
TwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Two" forIndexPath:indexPath];
if( cell == nil){
cell = [[TwoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Two"];
}
cell.twoLabel.text = #"HEHEHE";
return cell;
}
}
From the code that you show, your oneLabel and twoLabel are never initialized. If you want a quick fix, you can replace both of them with textLabel. Something as the following,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
OneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"One" forIndexPath:indexPath];
if( cell == nil){
cell = [[OneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"One"];
}
cell.textLabel.text = #"HAHAHA"; // Modified
return cell;
} else {
TwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Two" forIndexPath:indexPath];
if( cell == nil){
cell = [[TwoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Two"];
}
cell.textLabel.text = #"HEHEHE"; // Modified
}
}
And you will be able to see different text for two different sections in the table view. And they are indeed different TableviewCell classes.
However, if you want to use different labels for different UITableViewCell, then you have to make sure that they are initialized somehow somewhere. For example, you could overwrite the default UITableviewCell initializer in your custom table view cell. For example, in your OneTableViewCell.m file, add the following between #implementation and #end. In this case, you can use your original code in the UITableView class.
#implementation OneTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if ( self ) {
_oneLabel = [[UILabel alloc] init];
[self.view addSubView:self.oneLabel];
}
return self;
}
#end
I've looked around at other questions here and many other sources online and just can't find a solution. I have 3 rows and no matter what I do, they all keep the width defined in the tableview. The first one is what I want, but the second and 3rd I can't give a custom height.
Below is my code:
#import "DetailTableViewController.h"
#import "EventDetailTableViewCell.h"
#import "EventDescriptionTableViewCell.h"
#import "EventMapTableViewCell.h"
#interface DetailTableViewController ()
#end
#implementation DetailTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
static NSString *CellIdentifier = #"headerCell";
EventDetailTableViewCell *headerCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
headerCell.headerImage.image = [UIImage imageNamed:#"img4.png"];
return headerCell;
} else if (indexPath.section == 1) {
static NSString *CellIdentifier = #"descriptionCell";
EventDescriptionTableViewCell *descriptionCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
descriptionCell.descriptionImage.image = [UIImage imageNamed:#"img2.png"];
return descriptionCell;
} else {
static NSString *CellIdentifier = #"mapCell";
EventMapTableViewCell *mapCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return mapCell;
}
}
#pragma mark - Helper Methods
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 1) {
return #" ";
} else if (section == 2) {
return #" ";
} else {
return nil;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return 222;
} else if (indexPath.row == 1) {
return 80;
} else {
return 100;
}
}
#end
I believe there is a mistake in your code in heightForRowAtIndexPath. You have been using indexPath.sectionfor most of your TableView delegate and dataSource methods. So, I believe you want to use indexPath.section on heightForRowAtIndexPath as well.
The following code should work.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1) {
return 222;
} else if (indexPath.section == 2) {
return 80;
} else {
return 100;
}
}
I am getting a crash (app quits in iOS simulator) when I click on a cell. The error code is
"EXC_BAD_ACCESS (code = 1, address= 0x310cc493)
Here is the code:
// .h
#import <UIKit/UIKit.h>
#interface ChecklistsViewController : UITableViewController
#end
// .m
#import "ChecklistsViewController.h"
#interface ChecklistsViewController ()
#end
#implementation ChecklistsViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ChecklistItem"];
UILabel *label = (UILabel *)[cell viewWithTag:1000];
if (indexPath.row % 5 == 0) {
label.text = #"Walk the dog";
} else if (indexPath.row % 5 == 1) {
label.text = #"Brush my teeth";
} else if (indexPath.row % 5 == 2) {
label.text = #"Learn iOS development";
} else if (indexPath.row % 5 == 3) {
label.text = #"Soccer practice";
} else if (indexPath.row % 5 == 4)
label.text = #"Eat ice cream";
return cell;
}
- tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
} // Control reaches end of non-function
#end
In your example you do not allocate a UITableViewCell if none was dequeued. You need to do something like this:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ChecklistItem"];
if( cell == nil ) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"ChecklistItem"] autorelease];
// whatever additional initialization
...
}
You need to allocate the UITableViewCell in tableView: cellForRowAtIndexPath: method.
Your method should be like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UItableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UItableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//INSERT YOUR CODE
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UItableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UItableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//write your code here
return cell;
}