2015-12-05 48 views
0

我有一個帶有UITableView的Xamarin.iOS應用程序。視圖中只有一個部分。如果我將手指放在表格的一行上(比如第5行),暫時保持一段時間,然後滾動表格視圖,該行就會突出顯示。這是不太可取的,但沒有什麼大不了的。但是,如果我然後點擊另一行(比如說第10行)來選擇它,我的UITableViewSource對象就會獲得一個RowSelected回調函數,它與最初接觸到的行相同。這是一個很大的問題,因爲應用程序現在認爲用戶選擇了錯誤的行。此外,我無法看到任何以正確敲擊行到達的回叫。UITableViewCell在滾動過程中突出顯示,然後再選中

我試圖通過在滾動結束時取消選擇該行來解決此問題。它沒有幫助。

我有一個非Xamarin版本的應用程序沒有表現出這種行爲。

下面是一些我的代碼(通過自定義UITableViewSource子類),輸出:

public int HighlightedRowIndex {get;set;} = -1; 

public override void RowHighlighted(UITableView tableView, NSIndexPath rowIndexPath) { 
    // I actually don't want to be looking at highlighted at all; I'm just going here to try to understand/fix the problem. 
    CommonDebug.LogLine("row highlighted", (int)rowIndexPath.Row); 
    this.HighlightedRowIndex = rowIndexPath.Row; 
} 

public override void DraggingEnded(UIScrollView scrollView, bool willDecelerate) { 
    int highlighted = this.HighlightedRowIndex; 
    if (highlighted >= 0) { 
    NSIndexPath indexPath = NSIndexPath.FromRowSection(0, (nint)highlighted); 
    UITableView tableView = scrollView as UITableView; 
    if (tableView!=null) { 
     CommonDebug.LogLine("ATTEMPTING HACK FIX, BUT IT DOES NOT HELP"); 
     // tableView.SelectRow(indexPath, false, UITableViewScrollPosition.None); // This can be commented out, or not; it doesn't help either way. 
     tableView.DeselectRow(indexPath, false); 
    } 
    } 
    this.HighlightedRowIndex = -1; 
} 

public override bool ShouldHighlightRow(UITableView tableView, NSIndexPath rowIndexPath) { 
    CommonDebug.LogLine("should highlight", (int)rowIndexPath.Row); 
    // Returning false from here would prevent the problem, but would also prevent any selection . . . 
    return true; 
} 

public override void RowSelected(UITableView tableView, NSIndexPath indexPath) { 
    CommonDebug.LogLine("IOSListViewSource selected", indexPath.Row); 
    if (this.SelectIgnoreCount == 0) { 
    ListViewSectionModel model = this.GetModel(); 
    int row = (int)indexPath.Row; 
    model.SelectItem(row); 
    } else { 
    CommonDebug.LogLine("ignoring"); 
    } 
} 

輸出:

我把我的手指上5排,讓它留在那裏了一下,然後滾動表視圖:

should highlight 5 
row highlighted 5 
ATTEMPTING HACK FIX, BUT IT DOES NOT HELP 

然後我點擊行10:

WillSelectRow 5 
IOSListViewSource selected 5 

沒有跡象我可以發現第10行被竊聽。

有沒有人有同樣的問題/找到解決辦法?

回答

0

這個固定:

public int HighlightedRowIndex {get;set;} = -1; 

public override void DraggingEnded(UIScrollView scrollView, bool willDecelerate) { 
    int highlighted = this.HighlightedRowIndex; 
    if (highlighted >= 0) { 
    UITableView tableView = scrollView as UITableView; 
    if (tableView!=null) { 
     tableView.ReloadData(); 
    } 
    } 
    this.HighlightedRowIndex = -1; 
} 

public override void RowHighlighted(UITableView tableView, NSIndexPath rowIndexPath) { 
    this.HighlightedRowIndex = rowIndexPath.Row; 
} 
0

類似的方式這樣做是爲了自定義委託添加到UITableView的(列表)。我這樣做,因爲在我的情況下,UITableViewSource的子類是不好的主意。

定義自定義委託和重載滾動功能:

public class CustDelegate : UITableViewDelegate 
{ 
    private CustomListViewRenderer list { get; set; } 
    public CustDelegate(CustomListViewRenderer listRenderer) 
     : base() 
    { 
     list = listRenderer; 
    } 
    public override void Scrolled(UIScrollView scrollView) 
    { 
     if(list != null) 
      list.Scrolled(); 
    } 
} 

到UITableView中加入代表在ListRenderer

(UITableView)Control.Delegate = new CustDelegate(this); 

,並定義滾動功能,可以防止突出。

希望這對別人有用。

相關問題