0
我遇到以下問題。 我用自己的Cell創建了一個UITableView。 對於單元格,我創建了一個內部帶有4個標籤的xib文件。 我也創建一個自己的tableviewsource添加功能。ios以毫秒爲單位向UITableView添加數據
現在我有這個問題,每10毫秒我必須添加新的數據到表源,新的數據來自一個線程。 我必須顯示更新後的表格來源。 爲此,我使用
table.Reload();
當我嘗試更新表每次當我添加數據,在GUI塊總。
當我嘗試更新表格時,可以說每隔500毫秒,GUI就會每隔500毫秒阻止一次。 是否有任何「更好」的方式來顯示如此大量的數據。
我使用Xamarin Studio的Monotouch,但我也想從xcode獲得答案。 以下是一些代碼片段。
表格單元格:
public class CanMessageTableSource: UITableViewSource
{
public List<CanMesssageForGui> _messages;
bool groupMessages;
public CanMessageTableSource()
{
_messages = new List<CanMesssageForGui>();
}
public void ClearMessages()
{
_messages.Clear();
}
public void AddMessage(CanMessage message)
{
var incomingGuiMessage = message.ToGuiCanMessage();
_messages.Add(incomingGuiMessage);
}
public override int NumberOfSections (UITableView tableView)
{
return 1;
}
public override int RowsInSection (UITableView tableview, int section)
{
return _messages.Count;
}
public override float GetHeightForRow (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
return 16;
}
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
try
{
CanMessageCell cell = tableView.DequeueReusableCell("CanMessageCell") as CanMessageCell;
if (cell == null)
{
cell = new CanMessageCell();
var views = NSBundle.MainBundle.LoadNib("CanMessageCell", cell, null);
cell = Runtime.GetNSObject(views.ValueAt(0)) as CanMessageCell;
}
var msg = _messages.ElementAt(indexPath.Row);
cell.SetData(msg, GroupMessages);
return cell;
}
catch (Exception ex)
{
Logger.Log("CanMessageTableSource", "" + ex, HorschLibrary.Core.LogTypes.Error);
var cell = new CanMessageCell();
var views = NSBundle.MainBundle.LoadNib("CanMessageCell", cell, null);
cell = Runtime.GetNSObject(views.ValueAt(0)) as CanMessageCell;
if (_messages.Count >= indexPath.Row)
{
var msg = _messages.ElementAt(indexPath.Row);
cell.SetData(msg, GroupMessages);
}
return cell;
}
}
}
視圖控制器
partial class DeviceMessagesController : UIViewController
{
private BluetoothService _bluetoothService;
public DeviceMessagesController (IntPtr handle) : base (handle)
{
_tableSource = new CanMessageTableSource();
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
_bluetoothService = BluetoothService.GetInstance();
_bluetoothService.NewMessageOccurred += HandleNewMessageOccurred;
}
//will be called i worst case every milli second
void HandleNewMessageOccurred (object sender, IBluetoothMessage e)
{
if(e.MessageType == BluetoothMessageType.CanFrame)
{
_messageReceived = true;
var canMessage = ((BluetoothCanMessage)e).Message;
InvokeOnMainThread(()=>
{
_tableSource.AddMessage(canMessage);
tableMessges.ReloadData();
});
}
}
}
我應該在調用這個inserRowsAtIndexPaths的時候添加一個新消息給表源? – Daniel 2014-10-20 13:13:10
是的,我認爲這將是最簡單的方法。 – 2014-10-20 13:14:05
看起來不錯,是否還有一種方法可以在添加新消息時自動滾動到lates元素? – Daniel 2014-10-20 13:18:00