jamonholmgren / ProMotion

ProMotion is a RubyMotion gem that makes iPhone development less like Objective-C and more like Ruby.
MIT License
1.26k stars 147 forks source link

having problem style PM::TableViewCell with teacup, looking for help. #370

Closed anguskwan closed 10 years ago

anguskwan commented 10 years ago

i am refactoring my code using PM::TableScreen and PM::TableViewCell,the coding seems cleaner, happy..

but have a little problem integrating with teacup and cell. i had try 2 way to style the cell,

style CellClassName style view_id

but still fail.

can you guy point out what is my problem.? thanks.


class AuthorList < PM::TableScreen

    stylesheet :author_list

    include ViewFunctions
    include Common

    # # logic
    def on_load
        config_view
        init_vars
        reload_data
    end

    def init_vars
        @list_url = "#{Config::DOWNLOAD_URL}/authors.json"
        @page = 1
        @data = []

        @http_client = Http.new
    end

    def config_view
        table_view.frame = [[0,52],[320,568]]
                table_view.addPullToRefreshWithActionHandler -> { reload_data }
            table_view.addInfiniteScrollingWithActionHandler -> { load_more }
    end

    # table view
    def table_data
        [{
            cells: @data.map do |i|
                author = Author.new(i)
                # puts author.cn_name if Config::DEBUG
                {
                    cell_identifier: "AuthorCellIdentifier",
                    # title: author.cn_name,
                    title_text: author.cn_name,
                    author: author,
                    # action: -> { push AuthorDetail.new },
                    # arguments: { author: author, title_text: author.cn_name },
                    height: 50,
                    cell_class: AuthorCell
                }
            end 
        }]
    end

    def load_data
        @params = { :page => @page }
        @http_client.get(@list_url, @params) do |data|
          puts "Page: #{@page}" if Config::DEBUG
          if data.nil?
            App.alert("网络可能出问题了,请稍后重试!")
          else
            if @page == 1
              @data = data
            else
              @data += data
            end
            # puts "JSON Value: " + @data.to_s if Config::DEBUG
            update_table_data
            table_view.pullToRefreshView.stopAnimating
            table_view.infiniteScrollingView.stopAnimating
          end
          hide_msg
       end
    end

    def reload_data
        @page = 1
        load_data
    end

    def load_more
        @page += 1
        load_data
    end

end

class AuthorCell < PM::TableViewCell

    stylesheet :author_cell

    attr_accessor :title_text

    def layoutSubviews
        clean_subviews # resolve label overlap problem when scroll

        layout(self.contentView, :cell) do 
            @t = subview(UILabel, :author_title, frame: [[10,10],[250,30]], tag: 1)

            @t.text = @title_text
        end
    end

    def clean_subviews
        self.contentView.subviews.each do |s|
            s.removeFromSuperview
        end
    end

end

Teacup::Stylesheet.new(:author_cell) do 

    style :author_title, 
        color: :blue.uicolor

end

image

anguskwan commented 10 years ago

after put this code into style defination of tableview, solved.


    style :author_title, 
        color: :blue.uicolor
jamonholmgren commented 10 years ago

Nice! Glad it was an easy fix.