0

I haven't developed any iOS apps in a while. I am fine with both swift and Objective-C but what I find different is adding a UITableView to ViewController. Before, I used to add a UITableView to ViewController, add the required datasource methods and the typical UITableViewCell object, and return the cell in cellForRowAtIndexPath:, which would display empty cells depending on the number of rows I return. Now, I did everything the same, but the UITableView is empty and when I scroll I see the lines but not my cell.textlabel.text value, which I set. It seems now I am supposed to add UITableViewCell to the UITableView and remove the

   #pragma-mark TableView Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_formTitles count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(cell==NULL)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.textLabel.text=[_formTitles objectAtIndex:indexPath.row];
    cell.detailTextLabel.text=@"Kaushik";
    return cell;
}

I can't find a simple post online regarding the same. Can someone post what are the changes in the new iOS 9 in a simple manner? Thank you

screenshot

16
  • 1
    That style of code should still work. The new approach that has been available for a number of years now is to register a class against the reuse identifier and the dequeueReusableCellWithIdentifier will always return a cell, even if it needs to allocate one for you. Can you show more code?
    – Paulw11
    Jul 20, 2016 at 20:00
  • It seems now we need to add a mandatory uitableviewcell to the table and set a reuseidentifer isn't it? before we don't used to do that
    – koushik v
    Jul 20, 2016 at 20:07
  • If you are using a storyboard then you would add a prototype cell and set its reuse identifier. The framework will then register this class against the reuse identifier for you. If you are creating the tableview programatically then you don't need to register the class but it makes for less code in cellForRowAtIndexPath. You can also allocate and return cells yourself even if you are using a storyboard, but you wouldn't. Can you show your full cellForRowAtIndexPath?
    – Paulw11
    Jul 20, 2016 at 20:10
  • 1
    That code looks fine. How are you adding the tableview? Through nib? Storyboard? Code?
    – Paulw11
    Jul 20, 2016 at 20:15
  • 1
    This doesn't address your issue at all, and is just being pedantic, but you should compare against nil, not NULL, when comparison involves Objective-C objects. NULL is for pointer types.
    – jp2g
    Jul 20, 2016 at 20:17

2 Answers 2

0

Right..For those of you who are still prefer to add a tableview to a viewcontroller.Here are the steps

  1. In the ViewController drag and drop the tableview.Now instead of the lines which you see in the old Xcode.This time you would see a blank table view with the text " Table View prototype content" in the middle.

  2. We usually create a tableviewcell only we doing anything different like adding more labels or image etc but hereafter it is required to add a tableviewcell in the tableview.Drag and drop a tableview onto the tableview.It will display as protype cells.One can select type of cell here itself as basic,value 1,value 2 or subtitle.Also NEED TO SET THE REUSE IDENTIFIER IN THE STORYBOARD UTILITIES PANEL ON YOUR RIGHT. At the end, you can select the tableview and add the missing constraints.

  3. Implement the typical required datasource methods in your viewcontroller class.

    • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 5; }

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
      
          cell.textLabel.text=@"text";
          cell.detailTextLabel.text=@"DetailedText";
          return cell;
      }
      

The change here is that we dont require the usual if(cell==nil) {..} piece of code since we added the prototype cells to the storyboard.

Don;t forget to connect the delegate and datasource of the tableview to the viewcontroller.

This should display the tableview in your viewcontroller but i am getting a space on top of the tableview which i don't know why.Anyway this is how you add a tableview to a view controller using Objective-C language in iOS 9.3.

FYI: Guys if i've missed anything, please mention it in the comments

Thank you

0
  1. Put tableView to view controller and link it to viewcontroller.
  2. link delegate and datasource of tableview to viewcontroller.
  3. in

    viewcontroller.h

     @interface ViewController :<UITableViewDelegate, UITableViewDataSource>
    
  4. in viewWillAppear put

     tblService.delegate=self;
     tblService.dataSource=self;
    

    and your array elements.

    5.Impliment your tableview delegate method and datasource method .

    6.be sure you cell identifier equal the one put on storyboard.

this link will help you more to implement your first app and this video.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.