ios以毫秒为单位向UITableView添加数据
问题描述:
我遇到以下问题。 我用自己的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();
});
}
}
}
答
的问题是,你重装响应添加新的数据的所有行。你应该只添加的,而不是重新加载所有新行 - 以正常的方式做这将是调用
beginUpdates
insertRowsAtIndexPaths:withRowAnimation:
(因为你需要很多次)
endUpdates
在这样,表视图将不会重新加载所有数据,只需添加新行,这应该更有效。
我应该在调用这个inserRowsAtIndexPaths的时候添加一个新消息给表源? – Daniel 2014-10-20 13:13:10
是的,我认为这将是最简单的方法。 – 2014-10-20 13:14:05
看起来不错,是否还有一种方法可以在添加新消息时自动滚动到lates元素? – Daniel 2014-10-20 13:18:00