Example #1
0
bool NDUITableLayer::TouchMoved(NDTouch* touch)
{
    NDUILayer::TouchMoved(touch);

    if (m_sectionTitleVisibled)
    {
        for (unsigned int i = 0; i < m_sectionTitles.size(); i++)
        {
            NDUISectionTitle* sectionTitle = m_sectionTitles.at(i);
            CGRect rect = sectionTitle->GetScreenRect();
            if (CGRectContainsPoint(rect, m_beginTouch))
                return false;
        }
    }

    //this->SetFocusOnCell(m_curSection->GetFocusCellIndex());

    if (m_curSection)
    {
        CGPoint prePos = touch->GetPreviousLocation();
        CGPoint curPos = touch->GetLocation();
        CGFloat subValue = curPos.y - prePos.y;

        if (m_sectionTitles.size() > m_curSectionIndex)
        {
            NDUISectionTitle* sectionTitle = m_sectionTitles.at(m_curSectionIndex);
            CGRect sectionTitleRect = sectionTitle->GetFrameRect();

            if (m_curSection->Count() > 0)
            {
                if (subValue > 0)
                {
                    NDUINode* firstCell = m_curSection->Cell(0);
                    if (m_sectionTitleVisibled)
                    {
                        if (firstCell->GetFrameRect().origin.y + subValue  > sectionTitle->GetFrameRect().origin.y + sectionTitle->GetFrameRect().size.height)
                        {
                            subValue = sectionTitle->GetFrameRect().origin.y + sectionTitle->GetFrameRect().size.height - firstCell->GetFrameRect().origin.y + m_cellsInterval;
                        }
                    }
                    else
                    {
                        if (firstCell->GetFrameRect().origin.y + subValue  > sectionTitle->GetFrameRect().origin.y)
                        {
                            subValue = sectionTitle->GetFrameRect().origin.y  - firstCell->GetFrameRect().origin.y + m_cellsInterval;
                        }
                    }

                    if (subValue <= 0)
                        return false;
                }
                else
                {
                    NDUINode* lastCell = m_curSection->Cell(m_curSection->Count() - 1);
                    CGFloat sectionBottom = this->GetFrameRect().size.height;
                    if (m_curSectionIndex + 1 < m_dataSource->Count())
                    {
                        NDUISectionTitle* nextSectionTitle = m_sectionTitles.at(m_curSectionIndex + 1);
                        sectionBottom = nextSectionTitle->GetFrameRect().origin.y - m_cellsInterval;
                    }
                    if (lastCell->GetFrameRect().origin.y + lastCell->GetFrameRect().size.height + subValue < sectionBottom)
                    {
                        subValue = sectionBottom - lastCell->GetFrameRect().origin.y - lastCell->GetFrameRect().size.height - m_cellsInterval;
                    }

                    if (subValue >= 0)
                        return false;
                }
            }
        }

        if (m_backgrounds.size() > m_curSectionIndex)
        {
            NDUIRecttangle* cellBackground = m_backgrounds.at(m_curSectionIndex);
            for (unsigned int i = 0; i < m_curSection->Count(); i++)
            {
                NDUINode* uiNode = m_curSection->Cell(i);
                CGRect nodeRect = uiNode->GetFrameRect();
                uiNode->SetFrameRect(CGRectMake(nodeRect.origin.x, nodeRect.origin.y + subValue, nodeRect.size.width, nodeRect.size.height));
                bool bDraw = CGRectIntersectsRect(cellBackground->GetFrameRect(), uiNode->GetFrameRect());
                uiNode->EnableDraw(bDraw);
            }
        }


        if (m_scrollBars.size() > m_curSectionIndex)
        {
            if (m_scrollBarVisibled)
            {
                NDUIVerticalScrollBar* scrollBar = m_scrollBars.at(m_curSectionIndex);
                scrollBar->SetCurrentContentY(scrollBar->GetCurrentContentY() - subValue);
            }
        }

    }

    return true;
}
Example #2
0
void NDUITableLayer::OnSectionTitleClick(NDUISectionTitle* sectionTitle)
{
    if (m_dataSource)
    {
        for (unsigned int i = 0; i < m_sectionTitles.size(); i++)
        {
            NDUISectionTitle* section = m_sectionTitles.at(i);
            if (sectionTitle == section)
            {
                if (i == m_curSectionIndex && i+1 < m_sectionTitles.size())
                    m_curSectionIndex = i+1;
                else
                    m_curSectionIndex = i;
                m_curSection = m_dataSource->Section(m_curSectionIndex);
            }
        }

        for (unsigned int i = 0; i < m_sectionTitles.size(); i++)
        {
            NDUISectionTitle* section = m_sectionTitles.at(i);
            if (i == m_curSectionIndex)
            {
                section->OnTouchDown(true);
            }
            else
            {
                section->OnTouchDown(false);
            }
        }

        for (unsigned int i = 0 ; i <= m_curSectionIndex; i++)
        {
            CGRect sectionTitleRect = m_sectionTitles.at(i)->GetFrameRect();
            this->MoveSection(i, i * m_sectionTitlesHeight - sectionTitleRect.origin.y);
        }

        CGRect rect = this->GetFrameRect();
        for (unsigned int i = m_dataSource->Count() -1 ; i > m_curSectionIndex; i--)
        {
            CGRect sectionTitleRect = m_sectionTitles.at(i)->GetFrameRect();

            this->MoveSection(i, rect.size.height - sectionTitleRect.origin.y - sectionTitleRect.size.height);

            rect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height - sectionTitleRect.size.height);
        }

        //set enable draw
        for (unsigned int i = 0 ; i < m_dataSource->Count(); i++)
        {
            NDUIRecttangle* background = m_backgrounds.at(i);
            if (i == m_curSectionIndex)
                background->EnableDraw(true);
            else
                background->EnableDraw(false);

            NDSection* section = m_dataSource->Section(i);
            for (unsigned int j = 0; j < section->Count(); j++)
            {
                NDUINode* uiNode = section->Cell(j);

                if (i == m_curSectionIndex && CGRectIntersectsRect(background->GetFrameRect(), uiNode->GetFrameRect()))
                    uiNode->EnableDraw(true);
                else
                    uiNode->EnableDraw(false);
            }
        }

        //set focus
        this->SetFocusOnCell(m_curSection->GetFocusCellIndex());

    }
}
Example #3
0
void NDUITableLayer::ReflashData()
{
    if (!m_dataSource)
        return;

    m_sectionTitles.clear();
    m_backgrounds.clear();
    m_scrollBars.clear();

    for (unsigned int i = 0; i < m_dataSource->Count(); i++)
    {
        NDSection* section = m_dataSource->Section(i);
        for (unsigned int j = 0; j < section->Count(); j++)
        {
            NDUINode* uiNode = section->Cell(j);
            if (uiNode->GetParent())
            {
                uiNode->RemoveFromParent(false);
            }
        }
    }

    this->RemoveAllChildren(true);

    CGRect thisRect = this->GetFrameRect();
    CGSize winSize = NDDirector::DefaultDirector()->GetWinSize();

    //draw sections
    for (unsigned int i = 0; i < m_dataSource->Count(); i++)
    {
        NDSection* section = m_dataSource->Section(i);

        //draw background
        this->DrawBackground(i);

        //draw scrollbar
        if (m_scrollBarVisibled)
        {
            if (section->Count() > 0)
            {
                NDUIVerticalScrollBar* scrollBar = this->DrawScrollbar(i);
                //scrollBar->SetContentHeight((row - 1) * (section->GetRowHeight() + m_cellsInterval));
                CGRect lastCellRect = this->GetCellRectWithIndex(i, section->Count()-1);
                if (m_sectionTitleVisibled)
                    scrollBar->SetContentHeight(lastCellRect.origin.y + lastCellRect.size.height - (i + 1) * m_sectionTitlesHeight);
                else
                    scrollBar->SetContentHeight(lastCellRect.origin.y + lastCellRect.size.height);
            }
        }

        for (unsigned int j = 0; j < section->Count(); j++)
        {
            NDUINode* uiNode = section->Cell(j);

            //防止uilayer拦截行选择事件
            if (uiNode->IsKindOfClass(RUNTIME_CLASS(NDUILayer)) && !uiNode->IsKindOfClass(RUNTIME_CLASS(NDUITableLayer)))
            {
                NDUILayer* uiLayer = (NDUILayer*)uiNode;
                uiLayer->SetTouchEnabled(false);
            }

            CGRect cellRect = this->GetCellRectWithIndex(i, j);
            bool bDraw = CGRectIntersectsRect(m_backgrounds.at(i)->GetFrameRect(), cellRect) && (i == m_dataSource->Count() - 1);

            //draw cell
            uiNode->SetFrameRect(cellRect);
            uiNode->EnableDraw(bDraw);
            this->AddChild(uiNode);
        }
    }

    if (m_dataSource->Count() > 0)
    {
        m_curSectionIndex = m_dataSource->Count()-1;
        m_curSection = m_dataSource->Section(m_curSectionIndex);

        //draw section titles
        for (unsigned int i = 0; i < m_dataSource->Count(); i++)
        {
            this->DrawSectionTitle(i);
        }
        if (m_dataSource->Count() > 0)
        {
            NDUISectionTitle* sectionTitle = m_sectionTitles.at(m_curSectionIndex);
            sectionTitle->OnTouchDown(true);
        }
        this->SetFocusOnCell(m_curSection->GetFocusCellIndex());
    }
    else
    {
        m_curSection = NULL;
    }

}