MrS*_*eko 15 uitableview uiviewcontroller
我有一个这样的课:
@interface ExerciseLogDetails : UIViewController<UIActionSheetDelegate, UITableViewDelegate, UITableViewDataSource> {
Run Code Online (Sandbox Code Playgroud)
我试图显示一些元素,然后是UITextView.UITextView元素在Interface Builder上创建.执行此代码时:
- (void)viewDidLoad {
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:self.tableView];
}
Run Code Online (Sandbox Code Playgroud)
表显示,但不是我在Interface Builder中配置的表.它完全空白且未格式化.如何访问我的表并使用数据以程序方式填充它?
谢谢!
Fle*_*lea 18
这个帖子的一些提示帮助我创建了这个.我将提供一些更完整的代码文件,以帮助其他人:
步骤1.在故事板或XIB中将UITableView拖到View Controller上.在我的例子中,我正在使用故事板.
第2步:打开ViewController(在我的例子中只是DefaultViewController)并为UITableView添加两个委托: UITableViewDelegate和UITableViewDataSource.还为population和UITableView IBOutlet添加一个简单的数据源.
DefaultViewController.h
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *newsArray;
@end
Run Code Online (Sandbox Code Playgroud)
第3步:打开您的实现文件(DefaultViewController.m)并添加以下内容:
#import "DetailViewController.h"
@interface DetailViewController ()
- (void)configureView;
@end
@implementation DetailViewController
@synthesize newsArray;
@synthesize tableView;
#pragma mark - Managing the detail item
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
- (void)configureView
{
// Update the user interface for the detail item.
self.newsArray = [[NSMutableArray alloc] initWithObjects:@"Hello World",@"Goodbye World", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// typically you need know which item the user has selected.
// this method allows you to keep track of the selection
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
// This will tell your UITableView how many rows you wish to have in each section.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.newsArray count];
}
// This will tell your UITableView what data to put in which cells in your table.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifer = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer];
// Using a cell identifier will allow your app to reuse cells as they come and go from the screen.
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifer];
}
// Deciding which data to put into this particular cell.
// If it the first row, the data input will be "Data1" from the array.
NSUInteger row = [indexPath row];
cell.textLabel.text = [self.newsArray objectAtIndex:row];
return cell;
}
@end
Run Code Online (Sandbox Code Playgroud)
第4步:转到您的Storyboard或XIB并选择您的UITableView并将数据源和委托出口拖到DefaultViewController上以连接它们.此外,您还需要将UITableView 的引用插座连接到您在头文件中创建的IBOutlet tableView对象.
一旦完成,您应该能够运行它并且样本数据将到位.
我希望这个以及该线程上的其他技巧将帮助其他人在ViewController上从头开始设置UITableView.
Eya*_*yal 12
如果您在IB中配置了tableView,则不应该以编程方式创建一个tableView,您应该创建@property (nonatomic, retain) IBOutlet UITableView *tableView;
并将其连接到您在IB中配置的tableView.
尝试在tableView的
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
委托方法中设置断点,以查看是否调用此方法.
来自Apple UITableView文档:
UITableView对象必须具有充当数据源的对象和充当委托的对象; 通常,这些对象是应用程序委托,或者更常见的是自定义UITableViewController对象.数据源必须采用UITableViewDataSource协议,委托必须采用UITableViewDelegate协议.数据源提供UITableView在插入,删除或重新排序表的行时构造表和管理数据模型所需的信息.委托提供表使用的单元格并执行其他任务,例如管理附件视图和选择.
您可以看到,如果您没有将dataSource设置为tableView,则tableView将不知道如何显示以及显示什么,因此不会发生任何事情.
您可以通过调用tableView.dataSource = self;
或在IB中从tableView拖动到文件的所有者(即必须实现UITableViewDataSource
协议的viewController )来设置一个
UITableViewDataSource
协议中有两种方法,您的dataSource 必须实现:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)
和
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
如果您不实现这些方法,您将收到编译器警告.
如果实现UITableViewDelegate
协议,您可以更好地控制tableView的外观- 如行/页眉/页脚高度,选择等等...
来自Apple UITableView文档:
UITableView会覆盖UIView的layoutSubviews方法,以便仅在创建UITableView的新实例或分配新数据源时才调用reloadData.重新加载表视图会清除当前状态,包括当前选择.但是,如果显式调用reloadData,则会清除此状态,并且对layoutSubviews的任何后续直接或间接调用都不会触发重新加载.
创建tableView或分配新的dataSource时(或当你明确地调用它时),会调用ReloadData.
这是当tableView需要知道要显示什么(多少个部分?,多少行?,以及要显示哪个单元?) - 这就是numberOfRowsInSextion
调用方法的时候.
归档时间: |
|
查看次数: |
33132 次 |
最近记录: |