[英]How to set the subclass of a TableView that was created programmatically? (no storyboards)
I am using a custom framework and I need to set the subclass of my 'TableView' and 'TableViewCell' to the custom framework. 我正在使用自定义框架,我需要将“ TableView”和“ TableViewCell”的子类设置为自定义框架。 I would normally do this easily with the identity inspector but since I created everything programmatically, I do not know how to do it.
通常,我可以使用身份检查器轻松地执行此操作,但是由于我以编程方式创建了所有内容,因此我不知道该怎么做。 I am also not using storyboards.
我也没有使用情节提要。 Any tips?
有小费吗?
---edit---- - -编辑 - -
TableViewController.h 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 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
If you are instantiating the UITableView
yourself, just instantiate the library class instead. 如果您自己实例化
UITableView
,则只需实例化库类。 For example, if you said this: 例如,如果您这样说:
UITableView *tableView = [[UITableView alloc] initWithFrame:frame
style: UITableViewStylePlain];
then do this instead: 然后改为执行此操作:
LibraryTableView *tableView = [[LibraryTableView alloc] initWithFrame:frame
style:UITableViewStylePlain];
If LibraryTableView
provides some other, more specialized initWith…
method, you may need to use that instead. 如果
LibraryTableView
提供了其他一些更专门的initWith…
方法,则可能需要使用该方法。
If you're storing a reference to the table view in a property or instance variable, you may also want to change the type of that property or instance variable from UITableView *
to LibraryTableView *
. 如果要在属性或实例变量中存储对表视图的引用,则可能还需要将该属性或实例变量的类型从
UITableView *
更改为LibraryTableView *
。
I believe if you're using a UITableViewController
, you can create your own table view and assign it to the controller's tableView
property: 我相信,如果您使用的是
UITableViewController
,则可以创建自己的表视图并将其分配给控制器的tableView
属性:
UITableViewController *tvc = ...;
tvc.tableView = [[LibraryTableView alloc] initWithFrame:frame
style:UITableViewStylePlain];
In your viewDidLoad
, you're creating an FMMoveTableView
, but you're not storing it in the view controller's tableView
property. 在
viewDidLoad
,您正在创建FMMoveTableView
,但没有将其存储在视图控制器的tableView
属性中。 You must store it in the view controller's tableView
property, and when you do, the view controller will automatically set the table view's dataSource
and delegate
to self
. 您必须将其存储在视图控制器的
tableView
属性中,然后,视图控制器将自动设置表视图的dataSource
并将其delegate
为self
。
-(void) viewDidLoad{
self.tableView = [[FMMoveTableView alloc] init];
taskArray = [[NSMutableArray alloc] init];
}
In your tableView:cellForRowAtIndexPath:
, you need to instantiate an FMMoveTableViewCell
, not a UITableViewCell
. 在
tableView:cellForRowAtIndexPath:
,您需要实例化FMMoveTableViewCell
而不是UITableViewCell
。 You send the alloc
message to the class you want to instantiate: 您发送
alloc
消息,你想将类实例:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[FMMoveTableViewCell alloc]
initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:@"Cell"];
}
// etc.
If you have your own custom subclass of FMMoveTableViewCell
, instantiate that instead. 如果您有自己的
FMMoveTableViewCell
自定义子类,请实例化该实例。
I have created custom TableViewCells before, you can create a custom class just like you would for viewControllers (File > New > File). 我之前已经创建了自定义TableViewCells,可以像创建viewControllers一样创建自定义类(“文件”>“新建”>“文件”)。 When choosing which object to subclass, choose UITableViewCell.
选择要子类化的对象时,请选择UITableViewCell。
Unfortunately, Xcode will not give you the option to include a .xib file, so you'll have to create one of those too: File > New > File, but this time on the left side of the window that pops up, select 'User Interface' instead of 'Cocoa Touch'. 不幸的是,Xcode不会为您提供包含.xib文件的选项,因此您也必须创建其中一个文件:“文件”>“新建”>“文件”,但是这次在弹出的窗口的左侧,选择“用户界面”,而不是“可可触摸”。 Choose the 'Empty' icon at this screen.
在此屏幕上选择“空”图标。
You will be asked whether the device family is iPhone or iPad. 系统将询问您设备系列是iPhone还是iPad。 Since you are making a table view cell not a viewController, it doesn't matter which you pick.
由于您要使表格视图单元格不是viewController,因此选择哪个都没有关系。
When you create this object you will be presented with a blank canvas. 创建该对象时,将显示一个空白画布。 Drag a 'Table View Cell' from the object library to the canvas and you're set to start creating!
将“ Table View Cell”从对象库拖到画布上,就可以开始创建了!
It is of note that where you'd normally create outlets, you should create properties for TableViewCells. 值得注意的是,通常在哪里创建插座,应该为TableViewCells创建属性。 This is because other objects are going to be interacting with the TableViewCell, not just the TableViewCell's implementation file.
这是因为其他对象将与TableViewCell进行交互,而不仅仅是TableViewCell的实现文件。
Here is what a header file for a custom TableViewCell might look like: 自定义TableViewCell的头文件如下所示:
@interface MyCustomCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *icon;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UILabel *number;
@end
My implementation file was empty. 我的实施文件为空。
However, that's not all. 但是,还不是全部。 There is also some work to be done in the TableViewController that will utilize this cell.
在TableViewController中还需要完成一些利用此单元格的工作。 Be sure to import your TableViewCell's header file.
确保导入TableViewCell的头文件。
First, the cell has to be registered in the viewDidLoad method 首先,必须在viewDidLoad方法中注册该单元格
// Resiter the Nib
UINib *nib = [UINib nibWithNibName:@"MyCustomCell" bundle:nil];
[[self tableView] registerNib:nib forCellReuseIdentifier:@"MyCustomCell"];
And then when the TableViewController determines how to draw the cells: 然后在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]];
}
I hope this is helpful! 我希望这是有帮助的!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.