[英]How to set the subclass of a TableView that was created programmatically? (no storyboards)
我正在使用自定义框架,我需要将“ TableView”和“ TableViewCell”的子类设置为自定义框架。 通常,我可以使用身份检查器轻松地执行此操作,但是由于我以编程方式创建了所有内容,因此我不知道该怎么做。 我也没有使用情节提要。 有小费吗?
- -编辑 - -
TableViewController.h
#import <UIKit/UIKit.h>
#import "Tasks.h"
#import "Properties2ViewController.h"
#import "PKRevealController.h"
#import "FMMoveTableView.h"
#import "FMMoveTableViewCell.h"
#import "DetailViewController.h"
@interface ToDoTableViewController : UITableViewController <Properties2ViewControllerDelegate, UITableViewDelegate, FMMoveTableViewDataSource>
@property (strong, nonatomic) NSMutableArray *taskArray;
-(IBAction)addCell:(id)sender;
@end
TableViewController.m
#import "ToDoTableViewController.h"
@implementation ToDoTableViewController
@synthesize taskArray;
- (id)init {
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
UINavigationItem *i = [self navigationItem];
[i setTitle:@"Task List"];
[[i title] uppercaseString];
UIBarButtonItem *bbi = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addCell:)];
[[self navigationItem] setRightBarButtonItem:bbi];
[self.tableView setSeparatorColor:[UIColor colorWithRed:26.0/255 green:188.0/255 blue:156.0/255 alpha:1.0f]];
[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundColor:[UIColor colorWithRed:26.0/255 green:188.0/255 blue:156.0/255 alpha:1.0f]];
}
return self;
}
- (id) initWithStyle:(UITableViewStyle)style{
return [self init];
}
-(void) viewDidLoad{
FMMoveTableView *mtc = [[FMMoveTableView alloc]init];
[mtc setDataSource:self];
[self.tableView setDelegate:self];
taskArray = [[NSMutableArray alloc] init];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (!cell)
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"UITableViewCell"];
NSString *detailText = [NSString stringWithFormat:@"%.0f", [[taskArray objectAtIndex:[indexPath row]] timeInterval]];
[[cell textLabel] setText:[[taskArray objectAtIndex:[indexPath row]] taskName]];
cell.textLabel.text = [[[taskArray objectAtIndex:[indexPath row]] taskName] uppercaseString];
[[cell textLabel] setFont:[UIFont fontWithName:@"AvenirNext-DemiBold" size:15]];
[cell setBackgroundColor:[UIColor colorWithRed:236.0/255 green:240.0/255 blue:241.0/255 alpha:1.0f]];
cell.textLabel.textColor = [UIColor colorWithRed:26.0/255 green:188.0/255 blue:156.0/255 alpha:1.0f];
[[cell detailTextLabel] setText:detailText];
[[cell detailTextLabel] setFont:[UIFont fontWithName:@"Avenir-Black" size:16]];
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [taskArray count];
}
-(IBAction)addCell:(id)sender{
Properties2ViewController *pvc = [[Properties2ViewController alloc]init];
[pvc setDelegate:self];
[self presentViewController:pvc animated:YES completion:NULL];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[self tableView] reloadData];
}
-(void)properties2ViewControllerDidEnterPropertiesSuccesfully:(Tasks *)t{
if (![[t taskName] isEqual: @""]) {
[taskArray addObject:t];
}
[self.tableView reloadData];
}
-(void)moveTableView:(FMMoveTableView *)tableView moveRowFromIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{
[tableView moveRowAtIndexPath:fromIndexPath toIndexPath:toIndexPath];
[tableView reloadData];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Tasks *task = [taskArray objectAtIndex:[indexPath row]];
DetailViewController *dvc = [[DetailViewController alloc]init];
[dvc setTestTask:task];
[[self navigationController] pushViewController:dvc animated:YES];
// PKRevealController *pkrc = [[PKRevealController alloc]initWithFrontViewController:self rightViewController:dvc options:nil];
//[pkrc showViewController:dvc animated:YES completion:NULL];
}
-(void)loadView{
[super loadView];
}
@end
如果您自己实例化UITableView
,则只需实例化库类。 例如,如果您这样说:
UITableView *tableView = [[UITableView alloc] initWithFrame:frame
style: UITableViewStylePlain];
然后改为执行此操作:
LibraryTableView *tableView = [[LibraryTableView alloc] initWithFrame:frame
style:UITableViewStylePlain];
如果LibraryTableView
提供了其他一些更专门的initWith…
方法,则可能需要使用该方法。
如果要在属性或实例变量中存储对表视图的引用,则可能还需要将该属性或实例变量的类型从UITableView *
更改为LibraryTableView *
。
我相信,如果您使用的是UITableViewController
,则可以创建自己的表视图并将其分配给控制器的tableView
属性:
UITableViewController *tvc = ...;
tvc.tableView = [[LibraryTableView alloc] initWithFrame:frame
style:UITableViewStylePlain];
在viewDidLoad
,您正在创建FMMoveTableView
,但没有将其存储在视图控制器的tableView
属性中。 您必须将其存储在视图控制器的tableView
属性中,然后,视图控制器将自动设置表视图的dataSource
并将其delegate
为self
。
-(void) viewDidLoad{
self.tableView = [[FMMoveTableView alloc] init];
taskArray = [[NSMutableArray alloc] init];
}
在tableView:cellForRowAtIndexPath:
,您需要实例化FMMoveTableViewCell
而不是UITableViewCell
。 您发送alloc
消息,你想将类实例:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[FMMoveTableViewCell alloc]
initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:@"Cell"];
}
// etc.
如果您有自己的FMMoveTableViewCell
自定义子类,请实例化该实例。
我之前已经创建了自定义TableViewCells,可以像创建viewControllers一样创建自定义类(“文件”>“新建”>“文件”)。 选择要子类化的对象时,请选择UITableViewCell。
不幸的是,Xcode不会为您提供包含.xib文件的选项,因此您也必须创建其中一个文件:“文件”>“新建”>“文件”,但是这次在弹出的窗口的左侧,选择“用户界面”,而不是“可可触摸”。 在此屏幕上选择“空”图标。
系统将询问您设备系列是iPhone还是iPad。 由于您要使表格视图单元格不是viewController,因此选择哪个都没有关系。
创建该对象时,将显示一个空白画布。 将“ Table View Cell”从对象库拖到画布上,就可以开始创建了!
值得注意的是,通常在哪里创建插座,应该为TableViewCells创建属性。 这是因为其他对象将与TableViewCell进行交互,而不仅仅是TableViewCell的实现文件。
自定义TableViewCell的头文件如下所示:
@interface MyCustomCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *icon;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UILabel *number;
@end
我的实施文件为空。
但是,还不是全部。 在TableViewController中还需要完成一些利用此单元格的工作。 确保导入TableViewCell的头文件。
首先,必须在viewDidLoad方法中注册该单元格
// Resiter the Nib
UINib *nib = [UINib nibWithNibName:@"MyCustomCell" bundle:nil];
[[self tableView] registerNib:nib forCellReuseIdentifier:@"MyCustomCell"];
然后在TableViewController确定如何绘制单元格时:
- (UITableViewCell *) tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCustomCell"]
// Then here you can set the properties of each cell
[[cell label] setText:@"Some text here"];
[[cell number] setText:[NSString stringWithFormat:@"%d", 5]];
[[cell icon] setImage:[UIImage imageNamed:@"myImage]];
}
我希望这是有帮助的!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.