Monday, January 28, 2019

How to make ios UITableViewCell automatically resize based on content in label text

Usually the rows in UITableViewCell have fixed height based on cell prototype constraint design, however, sometimes, certain cell may include a large blob of text, such as term and conditions, privacy policy, etc, so the cell height needs to be automatically resized to fit the content.

The below code is required to automatically resize a default UITableViewCell object's height:

\\override two tableview callback methods
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

\\when creating the cell with long text content, set numberOfLine property to 0 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  
     var cell : UITableViewCell? = nil
     if (indexPath.row==1){
        
                cell = UITableViewCell()
                cell?.textLabel?.text = "Terms and Condition\r\n" +
                   "This is a long text string.\r\n" +
                   "This is a long text string.\r\n" +
                   "This is a long test string"
                cell?.textLabel?.numberOfLines = 0;
            }
     }
     return cell!

    }


Wednesday, January 23, 2019

javascript "if" condition check on undefined variable or property

In Javascript, when using if statement to check a variable's or a property's value is true or false, different result may happen depending whether the variable is declared, assigned or not.

If a javascript variable is not declared, then using if check on the variable will causes unreferenced exception
try
{
    if (someVariable){
         console.log("not get here")
    }
    else{
         console.log("not get here")
    }
}
catch(err){
     console.log("throw reference error: " +err);
}

if the variable is declared, but not defined, then the if check will return false

var someVariable1;
if (someVariable1) {
    console.log("if condition false")
}
else
{
  console.log("Get it here")

}

So for variable check, it is better to check typeof variable to undefined
try
{
    if (typeof someVariable5 == 'undefined'){
         console.log("get here")
    }
    else{
         console.log("not get here")
    }
}
catch(err){
     console.log("not get here: " +err);
}

var someVariable6;
if (typeof someVariable6 == 'undefined') {
    console.log("get here")
}
else
{
  console.log("Not get here")

}



For property, if the base object is undeclared or undefined, then exception will happen

try
{
    if (someVariable2.someProperty){
         console.log("not get here")
    }
    else{
         console.log("not get here")
    }
}
catch(err){
     console.log("throw reference error " + err);
}

var someVariable3;
try{
if (someVariable3.someProperty) {
    console.log("not get here")
}
else
{
  console.log("not get here")
}
}catch (err){
     console.log("throw type error " + err);
}


if the base variable has a value, then if check works

var someVariable4 = {};
if (someVariable4.someProperty) {
    console.log("if condition false")
}
else
{
  console.log("get here")
} 


For property check, first need to check the base variable is not undefined, which means it has a value assigned, and then check the property directly using if condition



try
{
    if (typeof someVariable7 != 'undefined' && (someVariable7.someProperty)){
         console.log("not get here")
    }
    else{
         console.log("get here")
    }
}
catch(err){
     console.log("not throw exception " + err);
}

var someVariable8;
try{
if (typeof someVariable8 != 'undefined' && (someVariable8.someProperty)) {
    console.log("not get here")
}
else
{
  console.log("get here")
}
}catch (err){
     console.log("not throw exception " + err);
}

var someVariable9 = {};
if (typeof someVariable9 != 'undefined'  && (someVariable9.someProperty)) {
    console.log("not get here")
}
else
{
  console.log("get here, property is undefined")

}