【问题标题】:Xcode / ObjectiveC - Convert UITableViewController into TableView embedded in a UIViewControllerXcode / ObjectiveC - 将 UITableViewController 转换为嵌入在 UIViewController 中的 TableView
【发布时间】:2013-10-23 02:21:36
【问题描述】:

我对这个 Native App 开发人员相当陌生 - 我已经构建了一个包含 UITableViewController 来显示消息的应用程序 - 一切正常 - 但出于样式原因,我需要将其从 TableViewController 更改为嵌入在 viewcontroller 中的 tableview。

我制作了一个包含表格视图和相关链接自定义单元格/字段的视图控制器,并将关联的头文件更改为 -

 @interface NotificationsListTVController : UIViewController

但我的表方法不再触发,我不确定如何实例化它们?

(代码如下) #pragma mark - 表格视图数据源

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

// Return the number of rows in the section.
return self.GPTNotifications.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{

static NSString *CellIdentifier = @"Cell";
static NSString *CellIdentifierRead = @"CellRead";

UITableViewCell *cell;


notifications *n = [self.GPTNotifications objectAtIndex:indexPath.row];



   if (n.read == false) {
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];



    CustomCellRead *cellReadB = (CustomCellRead *)cell;
    cellReadB.notifTitle.text = n.notifTitleD;
    cellReadB.notifDate.text = n.notifDateD;
    cellReadB.notifMsg.text = n.notifMessage;


 return cellReadB;
}

 else {
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierRead     forIndexPath:indexPath];



    CustomCell *cellReadB = (CustomCell *)cell;
    cellReadB.notifTitle.text = n.notifTitleD;
    cellReadB.notifDate.text = n.notifDateD;
    cellReadB.notifMsg.text = n.notifMessage;


    return cellReadB;

    }


 }

【问题讨论】:

标签: objective-c xcode


【解决方案1】:

您是否将 tableview 的委托和数据源设置为您的类?

类似:

self.myTableView.delegate = self;
self.myTableView.dataSource = self;

当您创建 UITableViewController 时,这已为您完成,但如果您自己添加表格,则需要设置它们。

还有:

@interface NotificationsListTVController : UIViewController <UITableViewDelegate, UITableViewDataSource>

【讨论】:

    【解决方案2】:

    我在Interface Builder中这样做:

    • 让你的TableViewController
    • 创建你的 ViewController 并添加一个 ContainerView 到它
    • 删除自带的segued嵌入式ViewController
    • 选择ContainerView 并建立从viewDidLoad 到您的TableViewController 的连接
    • 您只会获得一次选项:embed

    完成。您的 TableViewController 现在将显示在您的 ViewController 中。

    将您需要的任何数据从 ViewController 传递到具有嵌入式 Segue 的 TableViewController。

    【讨论】:

      【解决方案3】:

      在 NotificationsListTVController.h 中进行以下更改:

      @interface NotificationsListTVController : UIViewController<UITableViewDataSource,UITableViewDelegate>
      

      同样在 NotificationsListTVController.m 中,别忘了也提供这两个语句。

       tableView.delegate=self ;
       tableView.dataSource=self;
      

      这些是设置委托方法所必需的。这两个语句需要在初始化tableView之后提供。比如:

         tblView = [[UITableView alloc] initWithFrame:CGRectMake(100,200,320,420) style: UITableViewStyleGrouped];
      
      
         tblView.delegate = self;
         tblView.dataSource = self;
      
         [self.view addSubview:tblView];
      

      你所指的这些方法是委托方法,不像其他普通方法那样不能直接触发。

      希望对你有帮助!!!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-21
        • 2011-09-13
        • 1970-01-01
        • 2016-01-04
        • 1970-01-01
        • 2019-01-15
        • 2018-04-19
        • 1970-01-01
        相关资源
        最近更新 更多