3

In table list using swift here i want to load the one class table-list in another class view controller and this concept is working in objective-c but come down to swift delegate methods are not calling my objective -c@swift codes below please help me some one else

BackGroundView.h:-

#import <UIKit/UIKit.h>
@interface BackGroundView 

UIViewController<UITableViewDataSource,UITableViewDelegate>
{
     UITableView *tableView;
}
@property(nonatomic, retain) UITableView *tableView;
-(void)tableList:(UIView *) view1;
@end

BackGroundView.m:-

#import "BackGroundView.h"
@interface BackGroundView ()
{
    NSArray * Mainarray;
}
@end
@implementation BackGroundView
@synthesize tableView;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)tableList:(UIView *) view1
{
    Mainarray = [[NSArray alloc]initWithObjects:@"india",@"australia",@"usa", nil];

    tableView=[[UITableView alloc]init];
    tableView.frame = CGRectMake(0,0,320,400);
    tableView.dataSource=self;
    tableView.delegate=self;
    tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [tableView reloadData];
    tableView.separatorColor = [UIColor blackColor];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [view1 addSubview:tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return Mainarray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier   forIndexPath:indexPath] ;

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text= [Mainarray objectAtIndex:indexPath.row];

    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
@end

MaindView.h

#import <UIKit/UIKit.h>

@interface ViewController1 : UIViewController

@end

MaindView.m

#import "ViewController1.h"
#import "BackGroundView.h"

@interface MaindView ()
{
    BackGroundView * v1;
}

@end

@implementation MaindView

- (void)viewDidLoad {
    [super viewDidLoad];
    v1 = [[BackGroundView alloc]init];
    // Do any additional setup after loading the view.
    [v1 tableList:self.view];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

Now it's working fine in objective-c and table list is loading fine

come down to swift ios:-

Now i am calling TableViewAdding from Mainview class to BackgroundView it's calling but delegate methods are not calling in background class i.e it is showing empty view controller in swift table list is not loading properly please help me and this is my swift code

BackGroundView.swift

import UIKit
class BackGroundView: UIViewController,UITableViewDelegate, UITableViewDataSource  {

    var tableView: UITableView  =   UITableView()
    var items = NSArray ()

    override func viewDidLoad() {
        super.viewDidLoad()
}

   func TableViewAdding(myview:UIView)
    {
        items = ["india","australia","usa"];
        println(items)
        tableView.frame         =   CGRectMake(0, 50, 320, 200);
        tableView.delegate      =   self
        tableView.dataSource    =   self
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        myview.addSubview(tableView)
    }

   func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        println("numberOfRowsInSection")
        return self.items.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

         println("cellForRowAtIndexPath")

        var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

        cell.textLabel?.text = self.items.objectAtIndex(indexPath.row) as NSString

        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
        println("in first")
      }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

Mainview.swift

import UIKit

class Mainview: UIViewController{

    @IBOutlet var myview1: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        var total = BackGroundView.alloc()
        total.TableViewAdding(self.view)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
 }
2
  • can u test after adding numberOfSectionsInTableView delegate method in BackGroundView.swift Jun 19, 2015 at 7:10
  • Hello oldrinmendez numberOfSectionsInTableView and numberOfRowsInSection are calling but cellForRowAtIndexPath is not calling in this case please give me suggestion
    – user5026700
    Jun 19, 2015 at 8:42

3 Answers 3

2

[UPDATED] You're not initializing the BackGroundView in mainview.swift. You're just allocating the memory for the object. Note that you both alloc and init a class in Objective-C. In Swift, you have to do the same thing with Class.alloc().initialize(). However, swift has replaced that verbose line of code with a simple call: Class()

Objective-C:

Class *myInstance = [[Class alloc] init];

Swift:

var myInstance = Class()

Some other things:

  • It's always a good idea to call tableView.reloadData() after you change the information in it (like in your TableViewAdding) method.
  • It's never a good idea to hard code numbers (like the table view size).
  • TableViewAdding looks like a class method. tableViewAdding would follow the camelCase convention more accurately

Documentation for why using var myInstance = Class.alloc() is not the same as using var myInstance = Class() can be found in Apple's NSObject documentation under Creating, Copying, and Deallocating Objects

https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/index.html#//apple_ref/occ/clm/NSObject/alloc

2
  • Hello Pillip Trent i have already initialized BackgroundView in MainView i.e var total = BackGroundView() total.TableViewAdding(self.view) so what is the problem there?
    – user5026700
    Jun 19, 2015 at 8:30
  • You used the alloc() method, which does not initialize an object, it just allocates memory for it. For the same reason that you do [[Class alloc] init] in Objective-C and not just [Class alloc], you have to do var myInstance = Class.alloc() and then myInstance.initialize() Because that is a lot of code, they have replaced that entire allocating and initializing workflow into a simple line where you can do var myInstance = Class() NOTE: this is NOT the same as var myInstance = Class.alloc() Jun 19, 2015 at 9:58
1
  1. Using KVC to get delegate property.
  2. Using NSInvocation to call delegate method
1

Declare total as Class variable in Mainview.swift. Don't use .alloc()

import UIKit

class Mainview: UIViewController{

  var total = BackGroundView()
  @IBOutlet var myview1: UIView!

  override func viewDidLoad() {
      super.viewDidLoad()

      total.TableViewAdding(self.view)
  }
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
  }
}
0

Your Answer

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