How to create UITableView cells based on a condition?

Suppose I have eight cells in mine UITableView, but I want to display only those cells that meet a specific condition. I went through this and implemented mine numberOfRowsInSectionas:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int i = 0;
for (A2OActivityCounter *ac in activityCounters) {
    if(ac.startDate != nil) {
        i++;
    }
}
if (i == 0) {
    return 1;
   }
return i;
}

This way, it will return exactly the number of cells that match the criteria, and the one time I want in my table view. I want to know how to implement this in my table view. For example, there are five out of eight cells that match the criteria, so the number of rows returned will be five. But I want these five cells to be displayed, and there should not be an empty cell between these cells. Therefore, if the cells are similar to:

A - satisfies the condition B - satisfies the condition C - **does not** satisfies the condition D - satisfies the condition E - **does not** satisfies the condition F - satisfies the condition G - **does not** satisfies the condition H - satisfies the condition

, A, B, D, F H , . if (activityCounter.startDate != nil) cellForRowAtIndexPath, , , true, , (UITableViewCell *), ; t nil. - , !

+4
3

. activityCounters , . , , displayedActivityCounters, , .

displayedActivityCounters.

displayedActivityCounters activityCounters .

+4

ActivityCounters UITableView? :

@property (nonatomic, strong) NSMutableArray *filteredActivityCounters;

- (void)setupContent {

self.filteredActivityCounters = [NSMutableArray array];

for (A2OActivityCounter *ac in activityCounters) {
    if(ac.startDate != nil) {
        [self.filteredActivityCounters addObject:ac];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return self.filteredActivityCounters.count;
}

cellForRowAtIndexPath: :

A2OActivityCounter *ac = self.filteredActivityCounters[indexPath.row];
+2

You can hide the cell by executing this tableview delegate method as shown below:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    CGFloat height = 44.0;//suupose your default hieght is 44.0

    if (indexPath.row== cell c row || indexPath.row== cell E row  || indexPath.row== cell G row) {
        height = 0.0;
    }

    return height;
}
0
source

All Articles