Skip to content

Advanced Form Cell Extension

Instead of letting Mobile Development Kit wrap the view in a Form Cell, extension also can create a view which will be put into the Form Cell container directly.

The return value of the view() method must be class which implement an interface defined by Mobile Development Kit (ExtensionFormCell for iOS or com.sap.mdk.client.ui.fiori.formCell.views.IExtensionView for Android).

Example

In /app/extensions/controls/MyListPickerExtension.ts:

import app = require('tns-core-modules/application/application');
import { IControl } from 'mdk-core/controls/IControl';

declare var com: any;
declare var MyExtension: any;
export class MyListPickerExtension extends IControl {
  private _control: any;
  public view() {
    if (!this._control) {
      if (app.ios) {
        this._control = MyExtension.alloc().init();
      } else {
        this._control = new com.sap.extension.listpicker.MyListPickerExtension();
      }
    }
    return this._control;
  }

  public viewIsNative() {
    return true;
  }
  // ...
}

in MyListPickerExtension.java:

import com.sap.cloud.mobile.fiori.formcell.GenericListPickerFormCell;
import com.sap.mdk.client.ui.fiori.formCell.adapters.IBaseAdapter;
import com.sap.mdk.client.ui.fiori.formCell.models.BaseFormCellModel;
import com.sap.mdk.client.ui.fiori.formCell.views.IExtensionView;

public class MyListPickerExtension extends Object implements IExtensionView {
    // ...
    @Override
    public int getItemViewType() {
        return 999;
    }

    @Override
    public View createCell(ViewGroup parent) {
        return new GenericListPickerFormCell(parent.getContext());
    }

    @Override
    public IBaseAdapter createAdapter(BaseFormCellModel model) {
        return new MyListPickerAdapter(model);
    }
}

in MyExtension.swift:

import Foundation
import SAPFiori
import SAPMDC

@objc(MyExtension)
public class MyExtension: NSObject, ExtensionFormCell {
    var value: [Int]?
    public static var reuseIdentifier: String = "ExtensionFormCell"

    public func registerTo(_ tableView: UITableView) {
        tableView.register(FUIListPickerFormCell.self, forCellReuseIdentifier: MyExtension.reuseIdentifier)
    }

    public func dequeueReusableCell(for tableView: UITableView, atIndexPath indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MyExtension.reuseIdentifier, for: indexPath)
        if let cell =  cell as? FUIListPickerFormCell {
            cell.keyName = "Select"
            // cell.listPicker.isDataSourceRequiringUniqueIdentifiers = true

            let initValue = self.value
            let testValueOptions = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"]
            let dataSource = MyDataSource(options: testValueOptions)
            cell.listPicker.dataSource = dataSource
            cell.listPicker.searchResultsUpdating = dataSource
            cell.listPicker.register(FUIObjectTableViewCell.self, forCellReuseIdentifier: FUIObjectTableViewCell.reuseIdentifier)

            // show "None" in the placeholder if no pickeritem selected
            if initValue == nil || initValue?.count == 0 {
                cell.valueLabel.text = "None"
            } else {
                cell.valueLabel.text = self.value?.description
            }
            cell.onChangeHandler = { [unowned self] newValue in
                self.value = newValue
            }
        }
        return cell
    }
}

Last update: April 14, 2021