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;
}
}
Related
I creating the one UITable view with the 2 sections in with when i load the data into the uitablew it's working fine,
But my problem is how to resolve one sorting button is disable in section 0 and row 0
which more discribe into the following image.
please help to solve my problem
in advance thanking to your valuable time spend on my problem.
It's quite easy.
Listen on the UITableViewDataSource call tableView:canMoveRowAtIndexPath:
Like this:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0) {
return NO;
}
return YES;
}
If you don't not where to place the method, place it next to tableView:cellForRowAtIndexPath:
Set showsReorderControl to NO property of cell
cell.showsReorderControl = NO;
In addition, if the data source implements tableView:canMoveRowAtIndexPath: to return NO, the reordering control does not appear in that designated row. You need to check indexPath and return NO for it.
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.
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)
{
...
}
I was wondering if there is a way to make a UITableView editable in a way where the user is able to rearrange (move) individual TableViewCells, BUT without the "Remove-Cell"-Feature to appear.
thank you for your help
sam
Have you tried implementing the editingStyleForRowAtIndexPath method and returning UITableViewCellEditingStyleNone?
You change the if condition based on your requirement
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// in below if your write your condition on which delete is enable or disable
if ([[friednListArr objectAtIndex:indexPath.row][#"id"] isEqualToString:user.userId]) {
return UITableViewCellEditingStyleNone;
}
else{
return UITableViewCellEditingStyleDelete;
}
}
I'm beginner in Iphone!
Can I place many UITableView (greater 1) in a View? And how to control them?
You can place as many UITableViews into a parent view, but it's probably not a good idea. Each delegate and datasource method for a tableView takes that tableView as its first argument, so it's easy to tell them apart.
Well its not a good idea to have multiple tableviews in a view but if you want it you can have them and control the by there names like
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(tableView == declaredTableView1){
return 10;
}
else if(tableView == declaredTableView2){
return 11;
}
else {
return 12;
}
}
Hope this helps.