Ashish Ashish - 2 months ago 14
iOS Question

How to split text separated with "," on new UITableView custom cell in UITableView

Problem Statement:
I want to display text separated by "," on new custom

UITableViewCell
.

Problem: It displays all data in single custom cell only with multi-line property, as shown in below.

enter image description here

I want to display tableView like this way.

enter image description here

Now I'm trying to display above data separated by "," on each new custom cell, as shown in above screenshot, but it displays only 1st data and skip remaining data, as per my code.

let Meaning :String = "Aback,Abacus,Abandon,Able,Aboard"
let Smeaning :String = "Fabric,Habit,keen,Pace"

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

let cell = tableViewData.dequeueReusableCellWithIdentifier("cell")! as! StudentCell

let fmeaning = Mmeaning.characters.split{$0 == ","}.map(String.init)
let smeaning = Hmeaning.characters.split{$0 == ","}.map(String.init)

for var i = 0; i < fmeaning.count; i += 1{
print(fmeaning[i])
//Here it prints all values perfectly
}
for var i = 0; i < smeaning.count; i += 1{
print(smeaning[i])
//Here it prints all values perfectly
}


Problem occurs here below two statements: display only 1st value in
UITableView


cell.lblMeaning1.text = fmeaning[indexPath.row]
cell.lblMeaning2.text = smeaning[indexPath.row]
return cell;
}


How should I assign an
Array
to these two custom cells, so that it will display data on separate new custom cell?

Answer

For that you need to use section table with your tableView, also you need to make this calculation in viewDidLoad and then reload the tableView, For that declare your two instance of array like this in your viewController.

var languageDic = [String: [String]]()
var allLanguage = [String]()    

override func viewDidLoad() {
    super.viewDidLoad()
    let mMeaningArray = Mmeaning.characters.split{$0 == ","}.map(String.init)
    let hMeaningArray = Hmeaning.characters.split{$0 == ","}.map(String.init)
    self.languageDic = ["Marathi Meaning": mMeaningArray,"Hindi Meaning": hMeaningArray] // You can add other langaue in the dic sam way I have added this two
    self.allLanguage = Array(self.languageDic.keys) as [String]
    self.tableView.reloadData()
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return self.allLanguage.count
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String {
    return self.allLanguage[indexPath.section]
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        
    return self.languageDic[self.allLanguage[section]].count  
}

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

    let cell = tableViewData.dequeueReusableCellWithIdentifier("cell")! as! StudentCell
    let str = self.languageDic[self.allLanguage[indexPath.section]][indexPath.row]
    cell.lblMeaning1.text = str
    return cell
}

Note: In cellForRowAtIndexPath you need only one label.

Edit: As of you want return the count of array that have more element you can try like this. F

var mMeaningArray = [String]()
var hMeaningArray = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    self.mMeaningArray = Mmeaning.characters.split{$0 == ","}.map(String.init)
    self.hMeaningArray = Hmeaning.characters.split{$0 == ","}.map(String.init)
    self.tableView.reloadData()
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        
    return (self.mMeaningArray.count > self.hMeaningArray.count)? self.mMeaningArray.count : self.hMeaningArray.count  
}

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

    let cell = tableViewData.dequeueReusableCellWithIdentifier("cell")! as! StudentCell
    if indexPath.row < self.mMeaningArray.count {
        cell.lblMeaning1.text = self.mMeaningArray[indexPath.row]
    }
    else {
        cell.lblMeaning1.text = ""
    }
    if indexPath.row < self.hMeaningArray.count {
        cell.lblMeaning2.text = self.hMeaningArray[indexPath.row]
    }
    else {
        cell.lblMeaning2.text = ""
    }
    return cell
}
Comments