Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS create one static cell at the top of a dynamic table

The title pretty much says it. How do I create one static cell at the top of my dynamic table? I want to be able to use it as a legend for my table. Thanks.

Here is my tableView code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"Cell";
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"texture3.png"]];

TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    UIView *selectionView = [[UIView alloc] initWithFrame:CGRectMake(10, 7, 200, 65)];
    [selectionView setBackgroundColor: [UIColor clearColor]];
    cell.selectedBackgroundView = selectionView;
}

cell.callTypeLabel.text = currentCall.currentCallType;
cell.locationLabel.text = currentCall.location;
cell.unitsLabel.text = currentCall.units;
cell.stationLabel.text = [@"Station: " stringByAppendingString:currentCall.station];

if ([currentCall.callType isEqualToString:@"F"]) {
    cell.imageType = Fire;
}
else {
    cell.imageType = EMS;
}

if ([currentCall.county isEqualToString:@"W"]) {
    cell.imageType1 = Washington;
}
else {
    cell.imageType1 = Clackamas;
}

return cell;

}
like image 473
Jon Erickson Avatar asked Dec 13 '25 19:12

Jon Erickson


2 Answers

This seems more like you would want a header for your table, not a static cell. If i understand your question, you are requiring a cell that sits at the top and does not move, while the other cells in the dynamic part scroll up and down as per normal.

If this is the case, use the tableviewdelegate methods that return a header view and its size.

If this is not the case, then I didn't understand your question, sorry.

like image 74
Bergasms Avatar answered Dec 15 '25 14:12

Bergasms


I had a similar problem. I wanted a section of dynamic cells at the top and a few other sections at the bottom that were all static. I used a workaround, wherein I just manipulated the number of cells that would actually appear in the top section, even though the entire tableView is just a static tableView. I knew I would only use a maximum of 7 cells in the dynamic section of the table, so as long as you ensure that the maximum number of cells that will ever appear at one time in the dynamic section is <= 7 (or whatever number you choose), everything will work with no problem. Here's what you do:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
 int i;

switch(section) {
case 0:  
//Do a bunch of checking here on how many i actually need. Then, I will just return the required amount. 
//  As long as it is less than the number of cells I have statically placed in my .xib, everything works good.
     return i;
case 1:
     return 3;
case 2:
     return 1;
case 3:
     return 2;        
default:
     return 0;
}
}

And then in your willDisplayCell, customize the section or sections that are dynamic:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell*)currentCell forRowAtIndexPath:(NSIndexPath*)indexPath
{

  //  DB_NSLog((@"will display CELL"));

    if(indexPath.section==0){
        currentCell.textLabel.text=nil;
        for(int i=0;i<numberOfDevices;i++)
        {
            //get the names of connected devices from whatever they're array they are stored in
            //assign to respective labels
            //just using row indexes as tester for passing to the device specifiic settings page

            if(i==indexPath.row){
               //do whatever customization you want here

                currentCell.textLabel.text=[tempDic objectForKey:@"deviceName"];

               // currentCell.textLabel.textAlignment=UITextAlignmentCenter;
                currentCell.textLabel.font=[UIFont fontWithName:@"System" size:17];

                currentCell.textLabel.textColor=[UIColor colorWithRed:0 green:0.32156863 blue:0.54117647 alpha:1.0];


            }
        }

    }

Let me know if you have any questions!

like image 45
Tommy Devoy Avatar answered Dec 15 '25 13:12

Tommy Devoy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!