Load cell selectively in TableView - iphone

I need to return cell in tableView:cellForRowAtIndexPath: only when some condition is true,for example:
if (condition == true)
return nil;
else
return cell;
Returning nil gives me an error.

You'll need to do a little more to conditionally have a cell in a UITableView.
Let's assume you have 3 cells, and the first one is conditional. The first thing you need to do make your table have either 2 or 3 cells based on this condition:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(condition) {
return 3;
} else {
return 2;
}
}
Next you can return the actual cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(condition) {
if(indexPath.row == 0) {
//create and return conditional cell
} else {
//create and return normal cell
}
} else {
//create and return normal cell
}
}
And voila!
PS: (This is assuming everything is done in a single section. If you need to break that out into multiple sections we can get into that as well).

This is because you cannot have blank spaces in your UITableView, that is not allowed, you must at least return an empty cell. What are you trying to do?
The error presents when the TableView tries to retrieve the next cell and gets nil, it has to get the next cell no matter what.

Depending on exactly what you are intending to do here could you not do perform the conditional test before making a call to tableview:cellForRowAtIndexPath:
EG
if( someCondition )
{
[self.tableview cellForRowAtIndexPath:indexPath]
}
Or if your out come is to only display the table cells that meet a certain condition the I suggest you create a function that would copy those elements from your tableview data into an NSArray that you would use to display the desired/conditional table data.
IE
-(void)composeVisibleTableData
{
[m_visibleTableData removeAllObjects];
for( NSObject* dataObject in m_tableDataArray )
{
if( someCondition )//dataObject meetsCondition
{
m_visibleTableData addObject:dataObject];
}
}
}
Then in your UITableDelegate functions for numberOfRowsInSection: and tableview:cellForRowAtIndexPath: reference m_visibleTableData as the UITableView Data Source.

You should check condition when Cell's datasource is set :)
and Filter your data with condition.

Related

TableviewCell shows message when no data in table

I am using grouped tableview for developing a contact list using database. I have to show the message "No Contacts" on tableview when there is no contact in list. how can I do it?
Share your ideas..
Thanks in Advance
supposing that you are using an array to store all the contacts then use the following delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// You can also modify this condition according to a specific section
if([YOUR_ARRAY count] == 0)
{
return 1;
}
else
return [YOUR_ARRAY count];
}
Now adding data to table in following delegate
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Initialise your cell
if([YOUR_ARRAY count] > 0){
// add your array data to cells
}
if([YOUR_ARRAY count] == 0){
// this means no contacts in array and therfore you have only one cell to display NO CONTACTS
}
return cell;
}
For cases like this one we used table headers.
If the table had elements in his data source, the table header was clear and had a 1px height.
If the data source had no elements, then the table header view was set as big as the table's frame and contained a message, an image or whatever you might need.
The functions (table view delegate methods, actually) we used were height for header in section and view for header in section. We verified the data source inside the viewForHeader function
You can achieve the same effect using the table footers as well
you can add UILabel
ande set the text of the label
label.text = #"No results ";
and you make a test
if ([contacts count] == 0)
{
yourTableview.hidden = YES;
yourLabel.hidden = NO;
}
else
{
yourTableview.hidden = NO;
yourLabel.hidden = YES;
}`

Prevent cell from being created

In a tableview, I need to prevent a cell from being created by a given condition.
For example, I have a tableview that shows a couple of results but I want the app to filter them. Is a way to do this?
Best Regards.
Preventing a cell from being created doesn't sound good. The simplest way to do that is to implement correctly the ...numberOfRowsInSection: method given the condition :
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(condition) {
return theNumberOfRowsGivenTheCondition;
}
else {
return someOtherRowsCount;
}
}

Possible to tag a section in UITableView?

I have a uitableview with cells whose behavior depends on what:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 2) {
do x;
}else{
do y;
}
}
It kind of works when you are dealing with a single table, but when you try to add sections, or subclass things, this type of "magic numbers" break very easily.
My question is, is it possible to "tag" the section? so instead of doing section==1, we will do:
indexPath.section.tag=="user_stats" {load x}
indexPath.section.tag=="answers" {show answers}
indexPath.section.tag=="page" {show pagination}
Yeah, just set up an enum somewhere.
enum { kSectionUserStats = 0,
kSectionAnswers,
kSectionPage };
Then:
if(indexPath.section == kSectionPage)
{
// do x
} else if(indexPath.section == kSectionAnswers)
{
// do y
}
// etc.
This also lets you reorder your sections really easily just by changing their ordering in the enum.
If you are displaying section headers with titles, then the titles themselves can become your tags.
if([self titleForHeaderInSection:indexPath.section] == #"MySection1")
{
//do something
}
else
{
//do something else
}

Getting a cell number from cellForRowAtIndexPath

I'm making an iPhone app with a Table View, and I'm trying to place a different icon / image next to each cell on a table.
I know that you set the image in (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath with code that looks like this:
UIImage *image = [UIImage imageNamed:#"image.png"];
cell.imageView.image = image;
What I'm trying to figure out is, how do I target a specific cell, so that it has a unique image? Like:
if (cell.number == 0) {
//Use a specific image
}
else if (cell.number == 1) {
//Use a different image
Thanks!
The indexPath variable contains information about the cell's position. Modifying your example:
if (indexPath.row == 0) {
// Use a specific image.
}
See the NSIndexPath Class Reference and NSIndexPath UIKit Additions Reference for more information. It's also important to note that cell numbers reset in each section.
Use the row (and possibly also section) properties in the NSIndexPath passed to your tableView:cellForRowAtIndexPath: method to identify which cell is being queried.
this function is passed an index path, which has a section and a row. indexPath.row will pass back an integer you can check.
When cellForRowAtIndexPath is executed you have access to the indexPath variable, so if you want to customize the cell style depending on the cell index you can do something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
// code for cell 0
}
else {
if (indexPath.row == 1) {
// code for cell 1
}
}
}
This is just an example, I don't think that customizing your cells by using if conditions is the best idea, but it shows you how to do what you need.
Remember that indexPath contains the section of the table too. If you are using a Grouped table view, you need to manager the section too. For example:
if (indexPath.section == 0) {
// section 0
if (indexPath.row == 0) {
// code for section 0 - cell 0
}
else {
if (indexPath.row == 1) {
// code for section 0 - cell 1
}
}
}
else {
if (indexPath.section == 1) {
// section 1
if (indexPath.row == 0) {
// code for section 1 - cell 0
}
else {
if (indexPath.row == 1) {
// code for section 1 - cell 1
}
}
}
}
For a slightly nicer looking approach I would put all the images you want to use into an array:
_iconArray = #[#"picture1.png", #"picture2.png", #"picture3.png"];
This means that when you come to the cellForRowAtIndex function you can say only:
cell.imageView.image = [UIImage imageNamed:_iconArray[indexPath.row]];
This is also easier if you have more than one section, this time you can make an array of arrays, each containing the required pictures for the different sections.
_sectionsArray = #[_iconArray1, _iconArray2, _iconArray3];
cell.imageView.image = [UIImage imageNamed:_sectionsArray[indexPath.section][indexPath.row];
This immediately makes it very easy to modify the pictures (as you are only dealing with the arrays. And much easier if you have more rows and sections (imagine doing it manually for 100 rows)

How to get the indexPath of the particular table from multiple table in iphone

Hi i m using 2 tables in a single view and want to get the indexpath of the tables. I use
the below code to get the indexpath. but stil categoryId getting effected whatever table i access not the other thing. Please help...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView=categoryTblView)
{
categoryId=indexPath.row;
NSLog(#"categoryId=%d",categoryId);
}
else if(tableView=regionTblView)
{
regionId=indexPath.row;
NSLog(#"regionId=%d",regionId);
}
}
To test equality here, you need to use == instead of a single equal sign (which is for assignment).
So the code should be:
if (tableView == categoryTblView)
{
...
}
else if (tableView == regionTblView)
{
...
}