use*_*473 7 reusability uitableview ios
我想创建一个原型单元格,可以通过故事板在不同的表格视图中使用..这样做的正确方法是什么?任何指针赞赏.
mde*_*eus 10
我认为您不能创建原型单元并在故事板中的表之间共享它,但您可以在nib中创建原型单元,然后在ViewDidLoad方法中加载它,然后在表视图中使用它.这真的很简单,这里是如何......
A.添加nib文件:
1.选择New File ... 2.选择IOS - > User Interface
3.选择"Empty" - >这将为项目添加一个新文件.xib文件
4.将UITableViewCell从将浏览器对象放入xib文件并根据自己的喜好进行自定义
5.使用"实用工具"窗格更改属性 - >编辑笔尖与编辑故事板非常相似.
6.确保命名您的单元格 - 我选择了名称cellFromNib,但您可能还需要其他东西.
B.在每个表的viewDidLoad方法中加载UITableViewCell:
- (void)viewDidLoad
{
// load the cell in the nib - the nib can only contain this one UITableViewCell
[self.tableView
registerNib:[UINib nibWithNibName:[self @"nibFileNameWithoutExtension"]
bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:[self @"cellFromNib"]];
}
Run Code Online (Sandbox Code Playgroud)
C.将nib的tableViewCell排队...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellFromNib" forIndexPath:indexPath];
// customize your cell here...
}
Run Code Online (Sandbox Code Playgroud)
D.在故事板中为TableView添加一个"虚拟"原型单元格.当选择单元格时,从这个"虚拟"单元格中选择一个segue到你想要显示的视图 - 确保命名segue - 我将这个例子称为"theSegue".您将在代码中引用此segue.
E.最后,添加代码以从该单元格中删除...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// this is basically what the storyboard does under the hood...
// make sure you have a "dummy" prototype cell with this segue name for each
// tableview that uses this cell
[self performSegueWithIdentifier:@"theSegue" sender:self];
}
Run Code Online (Sandbox Code Playgroud)
如果要专门化单元代码,请创建一个子类UITableViewCell的类
我认为这就是你需要的一切.
我会说不要害怕做这样的事情,因为如果你认真对待IOS编程,你会学到新东西.它确实可以提供更好的可重用代码.
| 归档时间: |
|
| 查看次数: |
1858 次 |
| 最近记录: |