Change background color of suggestion cells

4 posts, 0 answers
  1. Armando
    Armando avatar
    4 posts
    Member since:
    Nov 2015

    Posted 19 Jul 2016 Link to this post

    I can change the suggestion view background color by calling

    self.autoCompleteTextView.suggestionView?.backgroundColor = UIColor.lightGrayColor()

     

    But the suggestion cells (?) remain white (see image attached below). I would like to change the appearance of these cells (font and background). How can i do that?.

  2. Deyan
    Admin
    Deyan avatar
    2108 posts

    Posted 22 Jul 2016 Link to this post

    Hello Armando,

    Thanks for writing.

    You could use the `settings` property exposed by TKDataSource. It accepts an object of type `TKDataSourceSettings` which in return exposes the `collectionView` property. This property accepts an instance of type `TKDataSourceCollectionViewSettings`. The `TKDataSourceAutoCompleteSettings` class has two methods:

    /**
     The settings that can be customized when using TKDataSource with UICollectionView.
     */
    @interface TKDataSourceCollectionViewSettings : NSObject
     
    /**
     Defines a block function that is called when creating a new cell in UICollectionView. TKCollectionViewCell is used by default.
      
     @param cellIdForItem The block function that will be called when creating a new cell.
     */
    - (void)createCell:(TKDataSourceCollectionViewSettings_CreatrCellBlock __nonnull)cellIdForItem;
     
    /**
     Defines a block function that is called when initializing the cell properties.
      
     @param initCellWithItem The block function that will be called when initializing a new cell.
     */
    - (void)initCell:(TKDataSourceCollectionViewSettings_InitCellWithItemBlock __nonnull)initCellWithItem;
     
    @end

    You can inherit from this class, override the methods and use them to style your cells. You will need to assign your custom implementation to the `collectionView` property of the TKDataSourceSettings object assigned to the TKDataSource instance you are using TKAutoCompleteTextView with.

    I hope this helps. 


    Regards,
    Deyan
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Philipp
    Philipp avatar
    5 posts
    Member since:
    Sep 2015

    Posted 25 Jan in reply to Deyan Link to this post

    I need to do it with C#:

     

    This doesn't work.

     

    dataSource.Settings.CollectionView.CreateCell(delegate (UICollectionView arg1, NSIndexPath arg2, NSObject arg3) {
                        return new UICollectionViewCell();
                    });
                    dataSource.Settings.CollectionView.InitCell((UICollectionView arg1, NSIndexPath arg2, UICollectionViewCell arg3, NSObject arg4) => {
                        arg3.BackgroundColor = UIColor.Clear;
                    });

  4. Deyan
    Admin
    Deyan avatar
    2108 posts

    Posted 30 Jan Link to this post

    Hello Philipp,

    You can try the following approach:

    1. Create a custom suggestion cell by inheriting from TKAutoCompleteSuggestionCell:

    public class PersonListViewCell : TKAutoCompleteSuggestionCell
        {
            public PersonListViewCell(IntPtr ptr) : base(ptr)
            {
                this.ContentView.BackgroundColor = new UIColor (0.91f, 0.91f, 0.91f, 1.0f);
            }
     
            public override void LayoutSubviews ()
            {
                base.LayoutSubviews ();
            }
        }

    As you can see, I am changing the background color of the content of the cell in the constructor.

    2. Use this cell as a suggestion cell in the AutoCompleteTextView:

    TKListView listView = (TKListView)autocomplete.WeakSuggestionView;
    listView.RegisterClassForCell (new Class(typeof(PersonListViewCell)), "cell");

    Let us know if you have additional questions here.


    Regards,
    Deyan
    Telerik by Progress
    Want to build beautiful Android apps as well? Check out UI for Android which enables the same set of scenarios, allowing you to create the same great app experience on both iOS and Android.
Back to Top