典型的 UITableView 使用模式是让主 UIViewController 成为它所持有的 UITableView 的目标数据源和委托。
是否有任何简单易懂的教程可以帮助我弄清楚如何将与 UITableViewDelegate 和 UITableViewDataSource 方法相关的代码移动到一个单独的类中,并将其挂接到我的 UIViewController 上?理想情况下,我希望委托和数据源位于同一个类中。
现在,我正在通过 Interface Builder 创建 UITableView 并将其出口连接到我的控制器类。
典型代码:
@interface MyController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UITableview *myTableview;
}
我想做更多这样的事情:
@interface MyController : UIViewController
{
IBOutlet UITableview *myTableview;
}
@end
@interface MyTableSourceDelegate : NSObject<UITableViewDelegate, UITableViewDataSource>
{
}
@implementation MyTableSourceDelegate
// implement all of the UITableViewDelegate and methods in this class
@end
我花了2个小时解决这个问题:
它对我有用
// GenreDataSource.h
#import Foundation/Foundation.h
@interface GenreDataSource : NSObject <UITableViewDataSource> {
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)