I wish to display a custom view in between a table view if a particular cell's attribute matches some condition.
Something like this
Row 1
Row 2
Row (n) (matches condition)
--------------------------- (Custom View)
Row (n+1)
Row (n+2)
I am using a TableView and TableViewDelegate in a ViewController
You also need TableDataSource with the delegate. The just return another cell class, depending on the condition of the object in source array in the tableView cellForRowAtIndexPath: method of the table delegate.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyObjectClass * objectFromArray = [sourceArray.objectAtIndex: indexPath.row];
if(condition on MyObjectClass or index row){
static NSString *MyIdentifier = #"MyReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
return cell;
}
}else{
static NSString *MyOtherIdentifier = #"MyOtherReuseIdentifier";
NewUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[NewUITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyOtherIdentifier];
}
return cell;
}
}
}
Related
I have two UITableView and I want to use the same custom cell for both UITableView.
The problem is only one of them works fine. Only the first table display rows.
When I debug I see the cell.lb_subject is nil for the second table.
The MyCustomCell was defined as prototype cell(Is NOT a xib) only on the first table, because I don't see the point to define the same row twice.
- (UITableViewCell *)tableView:(UITableView *)mytableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"MyCustomCell";
MyCustomCell *cell = [mytableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
MyRowData* data = [tableData objectAtIndex:indexPath.row];
cell.lb_subject.text = data.subject;
I have two UIViewController
One table per each UIViewController
I have not Xib, I have a storyboard.
I've uploaded to my repo a demo
Follow this step.
Your ViewController
ViewController.h
NSMutableArray *tableData;
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
tableData = [[NSMutableArray alloc]initWithObjects:#"test1",#"test2",#"test3",nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Here MyCustomCell is identifier for both tableview cell.
static NSString *simpleTableIdentifier = #"MyCustomCell";
//tblCell is your custom tableview cell.
tblCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[tblCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.lb_subject.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
Your Custom cell
tblCell.h
//Here lb_subject is common Outlet for both label of both cell.
#property (strong, nonatomic) IBOutlet UILabel *lb_subject;
Give Tag to your both tables and then use dequeueReusableCellWithIdentifier for both tables separately.
if (tableView.tag==1) {
static NSString *simpleTableIdentifier = #"MyCustomCell";
MyCustomCell *cell = [mytableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
MyRowData* data = [tableData objectAtIndex:indexPath.row];
cell.lb_subject.text = data.subject;
return cell;
}
else {
static NSString *simpleTableIdentifier = #"MyCustomCell";
MyCustomCell *cell = [mytableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
MyRowData* data = [tableData objectAtIndex:indexPath.row];
cell.lb_subject.text = data.subject;
return cell;
}
I would like to register the cell with the tableView in the class of customCell. something like this
class func cellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> YourCustomTableViewCell {
let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier"
tableView.registerNib(UINib(nibName: "YourCustomTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kYourCustomTableViewCellIdentifier)
let cell = tableView.dequeueReusableCellWithIdentifier(kYourCustomTableViewCellIdentifier, forIndexPath: indexPath) as! YourCustomTableViewCell
return cell
}
You can see a more detailed answer Here.
I am guessing that you are having two prototype cell for each of your table.
Let's say for, table A - "CustomCellA" & for table B - "CustomCellB"
both customCell's parent class should be same.
Now in cellForRowAtIndexPath write below code,
MyCustomCell *cell;
if (tableView == tableViewA)
cell = [mytableView dequeueReusableCellWithIdentifier:#"CustomCellA"];
else
cell = [mytableView dequeueReusableCellWithIdentifier:#"CustomCellB"];
Your cell's Outlet and attributes should be find for both tableview.
Do let me know the results.
Thanks, Hope this helps.
You've not added cell in other table
if you do not want to create another prototype cell for viewController 2 then you need to use XIB
Register your XIB :
Register your customcell in ViewDidload instead of cellForRow
[self.tableView registerNib:[UINib nibWithNibName:#"CustomCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:#"CustomCell"];
and,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = (CustomCell *)[topLevelObjects objectAtIndex:0];
}
cell.cellLabel.text = _arrData[indexPath.row];
return cell;
}
If you still face same problem then let me know
when I set UITableViewCellSeparatorStyleNone to a tableView, still the separatorview is visble?
I set the property of tableview,
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
and in view debug I still found a UITableViewCellSeparatorView in my cell,
How do I remove the separator view?
You can set UITableViewCellSeparatorStyleNone in tableview in storyboard.
Here i attach screenshot for more clarification.
since cells are being reused when presented (with dequeueReusableCellWithIdentifier): you have to use a different identifier for that cell.. I made a custom UITableViewCell subclass for it too.
this is a code where my last cell is a special cell that will load X more cells..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == lastIndex) {
LoadingNextCellView *cell = [tableView dequeueReusableCellWithIdentifier:#"LoadingNextCell"];
if (cell == nil) {
cell = [[LoadingNextCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"LoadingNextCell"];
}
cell.indexPath = indexPath;
cell.titleLabel.text = [NSString stringWithFormat:#"Loading next %d trees..",PRELOAD_TREES];
return cell;
} else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
}
return cell;
}
Customie your cell according to this logic.
I'm seeking how create several cells to go to different ViewControllers.
For my TableView, I'm using a subclass of UITableViewController.
And when I choose 2 in the following method, I just see 2 identical cells which are doing exactly the same thing. I'm not interested by this. I don't even know their IndexPath in order to change their title.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 2;
}
And When I try to put another UITableViewCell in my TableView, it doesn't appear on iOS simulator, even with the same option (same subclass) than my first UITableViewCell which I can see.
Thanks for your help.
Edit : Here is my new code to create 2 cells but doesn't work :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[customCell alloc] init];
}
static NSString *CellIdentifier1 = #"Cell1";
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell1 == nil) {
cell1 = [[customCell alloc] init];
}
// Configure the cell...
return cell;
}
You define your cells in tableView:cellForRowAtIndexPath:, so you should provide an implementation for that method.
tableView:numberOfRowsInSection: only returns the number of cells in the table.
If you need more help, please provide your implementation for tableView:cellForRowAtIndexPath:. This is how a typical implementation looks like:
- (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] autorelease];
}
... customize your cell ...
}
EDIT:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell2";
static NSString *CellIdentifier1 = #"Cell1";
if(indexPath.row == 0 ) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[customCell alloc] init];
}
} else {
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell1 == nil) {
cell1 = [[customCell alloc] init];
}
}
return cell;
}
This method gets called when a cell has been selected. You can decide what you wanna do according to the selected row
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
[self goToFirstViewController];
else
if(indexPath.row == 1)
[self goToSecondViewController];
}
Use the following:
- (NSInteger) tableView: (UITableView*) tableView numberOfRowsInSection: (NSInteger) section
This delegate method returns the number of rows you want in that particular section. So if you want more than 2 rows, or you want the number of rows to be dynamic, you can create a NSArray in the AppDelegate or in the init method of the viewController class, and return the number in the numberOfRowsInSection method like
return [delegate numberOfNames];
In my example above, I created an array in my AppDelegate and also a method to return the number of objects I have in that array so that I can create the number of rows for my table.
- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath
This delegate method will show what you want to display in each cell. Therefore, following on from my array created in my AppDelegate, I first create the cell, then I will set the text I want to display on the cell with a method I created in my AppDelegate that will return a NSString while taking in a NSInteger so that I can loop through my array and display the text accordingly.
static NSString* MyIdentifier = #"Default";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if( cell == nil )
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
cell.textLabel.text = [delegate nameAtIndex:indexPath.row];
}
nameAtIndex is the name of the method I created in my AppDelegate that will return the NSString object at the specific index (ie. the row number) from the NSArray I created to store all the items of my table.
When the user clicks on any of the rows in the table created, this delegate method will be called
- (void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath
And in here, I will check if the text displayed matches any of the items in my array from the AppDelegate that stores the items in the table, and create the view that is necessary.
UIViewController* viewController = nil ;
NSString* nameInArray = [delegate nameAtIndex:indexPath.row] ;
if( [nameInArray isEqualToString:#"firstName"] )
{
viewController = [[FirstViewController alloc] init];
}
else if( [nameInArray isEqualToString:#"secondName"] )
{
viewController = [[SecondViewController alloc] init];
}
else if( [nameInArray isEqualToString:#"thirdName"] )
{
viewController = [[ThirdViewController alloc] init];
}
So with these 3 delegate methods, you will be able to create the table using a NSArray created, and be able to redirect the user to a viewController according to which option in the table he chooses. You will not have to keep editing the delegate methods if you choose to add more rows to the table as well since you are returning the count of the array when setting up the table.
The array and methods to get the data of the array can be created in the viewController as well, not necessarily in the AppDelegate, in case you were wondering.
The methods are as follows:
-(NSInteger) numberOfNames
{
return [myArray count];
}
-(NSString*) nameAtIndex: (NSInteger) index
{
return [myArray objectAtIndex:index] ;
}
Hope this helps! :)
I have created an tableview with an array. There are 7 items in my array. But in cellForRowAtIndexPath method i'm unable to get indexpath.row == 6. What could be the reason.
Code is below for this.
-(void)viewDidLoad
{
[super viewDidLoad];
self.menu = [NSArray arrayWithObjects:#"Home",#"ansCategory",#"askQue",#"myQue",#"likedQue", #"topQue", #"topUser",nil];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
// NSLog(#"count::::::%d",self.menu.count);
return self.menu.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
NSLog(#"rows:%#",indexPath);
NSLog(#"row:%#",[self.menu objectAtIndex:indexPath.row]);
}
return cell;
}
I'm getting following out put:
[here] (https://www.dropbox.com/s/pz6t34xr7l4glxz/menu1.png)
first index is overlaying on last one.
The cell is reused.
Put your log statement outside of the if(cell=nil):
Here is the working example for outputting the contents of the array in a tableview. Item with index 6 which is the seventh in the array ("topUser") is in red.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
NSLog(#"rows:%#",indexPath);
NSLog(#"row:%#",[self.menu objectAtIndex:indexPath.row]);
cell.textLabel.text = self.menu[indexPath.row];
if (indexPath.row == 6)
cell.textLabel.textColor = [UIColor redColor];
return cell;
}
For N items the last indexpath is (N-1)
Thus, if you have 10 items, then the index path of last item is 9 because the indexpath of 1st item in array is 0.
indexPath of items starts with 0 and not 1.
Try to access the index path in your answer like below in your code
Instead of :
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
NSLog(#"rows:%#",indexPath);
NSLog(#"row:%#",[self.menu objectAtIndex:indexPath.row]);
}
Write
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
//Access the indexPath outside here...
//Because once the cells are created and not nil
//then the above scope will be bypassed.
NSLog(#"rows:%#",indexPath);
NSLog(#"row:%#",[self.menu objectAtIndex:indexPath.row]);
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
gets executed whenever the new row is needed to be created and added to the tableview.The tableview populates row such that when it needs to be dispayed on screen it is created by calling the very same method.So If your 7 th row is not visible on screen the method wont get executed for row 7 ie indexpath.row 6 and so to get the method to log the 7 th row just scroll down so that that cell can be made visible by the table and it will execute the method and you can have the log
I changed my code to following line and it worked.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
I'm trying to write an app that connects to my web service and displays data in a table view. I also have a static table view for a menu. The static table view works (responds to clicks, shows options, etc.) but I'm stumped on getting the second table view working. There is no more than 1 table view per window. I'll be adding about 10 table views.
Code for static table view:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"dataSelect"; //Name of table view
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
tableData is the array that I'm inserting.
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"dataSelect"; //Name of table view
if (tableView == tableview1) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
} else if (tableview == tableview2) {
// do actions related to tableview 2 here
} else {
// and so on
}
}