Simple way to separate UITableview datasource and delegate from main UIViewController class?

前端 未结 4 1074
终归单人心
终归单人心 2021-01-02 08:19

The typical UITableView usage pattern is to have the main UIViewController become a target datasource and delegate for the UITableView it is holding on to.

Are there

4条回答
  •  暖寄归人
    2021-01-02 09:00

    I spend 2 hours to solve this problem:

    It's working for me

    //  GenreDataSource.h
    
    #import Foundation/Foundation.h
    
        @interface GenreDataSource : NSObject  {
            NSArray *dataSource;
            CGSize cellSize;
        }
    
    @property(nonatomic, assign) CGSize cellSize;
    
    @end
    
    
    
    //  GenreDataSource.m
    #import "GenreDataSource.h"
    
    @implementation GenreDataSource
    @synthesize cellSize;
    
    -(id)init{
    
        self = [super init];
        if ( self != nil ) {
    
            dataSource = [[NSArray alloc] initWithObjects:@"All",@"Folk",@"Disco",@"Blues",@"Rock",@"Dance",@"Hip-Hop",@"R&B",@"Soul",@"Lounge",@"Techno",@"Bubstep", nil];
        }
        return self;
    }
    
    #pragma mark - UITableViewDataSource
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return [dataSource count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *CellIdentifier = @"CellPicker";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
    
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease];
            [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
    
            //сконфигурируем структуру
            FontLabel *fLabel= [[FontLabel alloc] initWithFrame:CGRectMake(30, 
                                                                           5, 
                                                                           cellSize.width-30, 
                                                                           cellSize.height-5)
                                                       fontName:@"HelveticaNeueCondensedBlack" 
                                                      pointSize:18.0f];
            [fLabel setTextColor:[UIColor darkTextColor]];
            [fLabel setTag:101];
            [fLabel setBackgroundColor:[UIColor clearColor]];
            [cell.contentView addSubview:fLabel];
            [fLabel release];
        }
    
        FontLabel *fLabel = (FontLabel*)[cell viewWithTag:101];
        [fLabel setText:[dataSource objectAtIndex:indexPath.row]];
    
        return cell;
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 1;
    }
    
    @end
    

提交回复
热议问题