如何在 .xib 文件上创建的 UIViewController 中设置 UITableView

2024-03-03

我有一堂这样的课:

@interface ExerciseLogDetails : UIViewController<UIActionSheetDelegate, UITableViewDelegate, UITableViewDataSource> {

我试图在其中显示一些元素,然后显示 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];
}

一张表显示了,但不是我在 Interface Builder 中配置的表。它是完全空白且未格式化的。如何访问我的表并以编程方式填充数据?

谢谢你!


该线程上的几个技巧帮助我创建了这个。我将提供一些更完整的代码文件,以帮助其他人:

步骤 1. 将 UITableView 拖到 Storyboard 或 XIB 中的视图控制器上。在我的示例中,我使用故事板。

第 2 步:打开 ViewController(在我的例子中只是默认 ViewController)并为 UITableView 添加两个委托:UITableViewDelegate and UITableView数据源。还添加一个简单的人口数据源和 UITableView IBOutlet。

默认ViewController.h

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *newsArray;

@end

步骤 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

第 4 步:转到 Storyboards 或 XIB 并选择 UITableView 并将数据源 and delegate插座到您的默认视图控制器将它们连接起来。您还需要连接参考插座将 UITableView 添加到您的IB出口表视图您在头文件中创建的对象。

完成后,您应该能够运行它并且示例数据将就位。

我希望这与该线程上的其他技巧一起能够帮助其他人在 ViewController 上从头开始设置 UITableView。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 .xib 文件上创建的 UIViewController 中设置 UITableView 的相关文章

  • UITableView框架改变动画问题

    我用谷歌搜索了很多这个问题 但似乎没有答案 所以我希望你们中的一些人知道如何处理这个问题 我有一个具有 tableview 的视图控制器 当我用动画更改视图框架时 一切都很顺利 除了一种特殊情况 当 tableview 的项目多于屏幕大小时
  • 处于编辑模式时滑动即可删除

    我有一个使用 uitableview 的 iPhone 应用程序 我希望始终显示 重新排序 控件 并让用户滑动以删除行 我目前采取的方法是将表格视图置于编辑模式并允许在编辑模式下进行选择 self tableView editing YES
  • iPhone UITableView 分页结果

    对从服务器拉取的大量结果进行分页的最佳方法是什么 就服务器而言 我可以抵消和限制结果 因此我一次只能提取 25 个结果 但是允许用户查看更多结果而不需要像应用商店一样不断向下滚动不断增长的列表的最佳方式是什么应用程序 谢谢 豪伊 要在列表底
  • 当应用程序退出活动状态时,MPMovies PlayerViewController 被解雇

    当我将 iPhone 设置为睡眠状态 切换到另一个应用程序等 然后再次返回时 之前的可见内容MPMoviePlayerViewController 提出与presentMoviePlayerViewControllerAnimated 已经
  • 覆盖层不与 UITableView 一起滚动 - iOS

    我有一个 UITableView 类 它使用以下方法在转到下一个屏幕时调用加载覆盖 问题是这个加载屏幕不随列表滚动 所以如果你滚动一点并单击某些东西 加载屏幕不会显示 因为它位于顶部 如何让加载屏幕始终保持在 UITableView 的顶部
  • iOS - UITableViewCell 使文本加粗

    我有一个字符串 NSString userInfo James Johnson james 我想做的就是大胆James Johnson并保留 james正常字体 所以我尝试过的是使用NSAttributedString但为了完成这个过程 我
  • cellForRowAtIndexPath 中的框架没有变化

    我想改变x位置框架view细胞内的cellForRowAtIndexPath对于某些条件 我使用了以下代码 但并不改变看法x position frame void viewDidLoad super viewDidLoad UINib n
  • 如何呈现半屏模态视图?

    我有一个 UIViewController 当按下按钮时 我想要一个半屏视图向上滑动 其中有一个 UIPicker 我在 IB 中使用 UIPicker 和带有 完成 取消 按钮的 UIToolBar 制作了一个 UIView 我怎样才能做
  • UITableViewCell 的 viewDidAppear

    我通常使用viewDidAppear方法在视图完成出现后在视图上执行一些 UI 操作 我在各种情况下使用了此方法 它非常有用 但是 我需要在视图上进行一些 UI 更改UITableViewCell当它完成出现后 SDK中是否有任何可用的方法
  • 您有什么方法可以从相机胶卷转到新的视图控制器吗?

    我正在尝试从相机胶卷转到新的视图控制器 所以基本上我希望能够选择一张图片 并且在选择图片后 它将显示在新视图控制器上的 imageView 上 那个很难做吗 void imagePickerController UIImagePickerC
  • 使用 UItableviewCell 实现 Google 地图

    我正在尝试在 UItableviewCell 组件内实现谷歌地图 我这样做的方法是在原型单元中定义 GMSMapView 然后使用 dequeueReusableCell 方法配置地图单元 但是 我尝试应用的任何更改都会失败 例如添加标记
  • 如何在 UITableView 中显示零行的表格

    我正在动态地将内容加载到 UITableView 中 如果有数据 表格需要显示数据 如果没有数据 表格应显示普通页面 但在我的应用程序中 表格显示带有两条分隔线的普通页面 我需要删除此分隔线并显示纯白色页面 请建议 任何帮助 将不胜感激 如
  • TableViewController 的 viewDidLoad 未触发

    我一直在关注这个tutorial http www appcoda com ios programming sidebar navigation menu 有一个滑出式菜单 我添加了一个 TableViewController 它将显示文章
  • tableView:canEditRowAtIndexPath: 弹出 viewController 时崩溃

    我有一个带有UITableView 我允许通过滑动来编辑 删除 的行 就像在邮件应用程序中一样 我用以下方法来做到这一点 BOOL tableView UITableView tableView canEditRowAtIndexPath
  • 自定义 UITableViewCell 不显示故事板中的标签

    在此屏幕截图中 您可以看到我已在 UIViewController 中添加了 UITableView 然后通过在其中添加一些标签来自定义 UITableViewCell 但问题是当我运行应用程序时 所有单元格都是空的 根本没有标签 我不明白
  • TableView 单元格重用和不需要的复选标记 - 这简直要了我的命

    苹果的iOSTableView细胞重复使用简直要了我的命 我搜索 搜索 研究 但找不到好的文档或好的答案 问题是当TableView重用单元格诸如在选定单元格上设置的复选标记 单元格附件 之类的内容会在表视图中更下方的单元格中重复 我知道
  • 如何在第二个 ViewCcontroller 中使用第一个 ViewController 的解析元素?

    在我的应用程序中我发送一个GET到我的服务器并收到一些响应 我有来自主视图控制器类的 TavleView 和 TableViewController 类 我在主 ViewController 中进行解析 我想用从第一个 ViewContro
  • iPad 上的 UITableView 背景颜色始终为灰色

    当我设置backgroundColor for my UITableView它在 iPhone 设备和模拟器 上运行良好 但在 iPad 模拟器上运行不佳 相反 我设置的任何颜色都会有浅灰色背景 包括groupTableViewBackgr
  • 从呈现的视图控制器访问呈现的视图控制器?

    我有一个视图控制器 包含我的菜单 显示在另一个视图控制器 我的应用程序 之上 我需要从呈现的视图控制器 我的菜单 访问呈现的视图控制器 在我的菜单下方 例如访问某些变量或使呈现的视图控制器执行其segues之一 但是 我就是不知道该怎么做
  • 我如何知道 UITableView 何时完成 ReloadData?

    我试图在执行完成后滚动到 UITableView 的底部 self tableView reloadData 我原本有 self tableView reloadData NSIndexPath indexPath NSIndexPath

随机推荐