2017-04-06 45 views
0

专家,删除行数据源问题 - 无效更新:行数无效在第0与Xamarin.iOS

我有一个从一个JSON文件中的一些细胞填充一个UITableView。我希望用户轻扫以删除特定的单元格。我使用SWTableViewCell组件实现了删除功能,并且几乎让它解决了一个问题:“无效更新:节0中的行数无效”。我怀疑我需要从我的模型中的数据源和我的JSON文件中删除对象,但不知道如何去做。提前致谢!

下面是执行:

public override void DidTriggerLeftUtilityButton(SWTableViewCell cell, nint index) 
     { 
      // Delete button was pressed 

      switch (index) 
      { 
       case 0: 

        NSIndexPath cellIndexPath = tableView1.IndexPathForCell(cell); 
        dataList.RemoveAt(cellIndexPath.Row); 
        tableView1.DeleteRows(new[] { cellIndexPath }, UITableViewRowAnimation.Left); 

        break; 
      } 
     } 

public override nint RowsInSection(UITableView tableview, nint section) 
     { 
      return dataList.Count; 
     } 

这里是填充JSON模式:

public class DataLocalNotifications 
{ 
    public string name { get; set; } 
    public string NotificationMessage { get; set; } 
    public string date { get; set; } 
    public string logo { get; set; } 
    public double latitude { get; set;} 
    public double longitude { get; set;} 
    public string website { get; set; } 
    public double phone { get; set; } 
    public string yelp { get; set; } 

    public DataLocalNotifications() 
    { 
    } 

} 

    public class LocalNotificationsModel 
{ 
    List<DataLocalNotifications> _model = new List<DataLocalNotifications>(); 
    public List<LocalNotificationsModel> businessList = new List<LocalNotificationsModel>(); 
    public static LocationManager Manager { get; set; } 
    public double distance; 
    CLLocationManager location; 
    public double Lat { get; set; } 
    public double Long { get; set; } 

    public DataLocalNotifications this[int index] 
    { 
     get { return _model[index]; } 
     set { _model[index] = value; } 
    } 

    public List<DataLocalNotifications> data 
    { 

     get { return _model; } 
     set { _model = value; } 
    } 

    public int Count 
    { 
     get { return _model.Count; } 
    } 

    public void Add(DataLocalNotifications item) 
    { 
     _model.Add(item); 

    } 

    public static LocalNotificationsModel Init() 

    { 
     LocalNotificationsModel model = new LocalNotificationsModel(); 
     string documentsPath1 = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 

     var localPath1 = Path.Combine(documentsPath1, "Notifications.json"); 
     var project1= JsonConvert.DeserializeObject<RootObjectNotifications> (System.IO.File.ReadAllText (localPath1)); 
     //project1.Notifications.Reverse(); 
     foreach (var bus in project1.Notifications.OrderByDescending(x => x.UnixTime)) 
     { 

      model.Add(new DataLocalNotifications 
      { 
       NotificationMessage = bus.BusinessNotification, 
       name = bus.Name_Location, 
       date = bus.Date, 
       logo = bus.BusinessImage, 
       latitude = bus.Latitude, 
       longitude = bus.Longitude, 
       website = bus.Website, 
       phone = bus.Phone, 
       yelp = bus.Yelp 
      }); 

     } 
     return model; 

    } 
+0

为什么不使用STANDAR滑动删除? – XTL

+0

标准滑动删除,我会得到相同的结果。我使用该组件的优点很多。 – pkozlowski

+0

嗯..我可以展示如何通过本地的东西(没有组件)来实现它。如果你愿意,我会提供样品。 – XTL

回答

1

好吧,让我们尝试一个简单的例子。

的ViewController拖放有的TableView(也,你可以在上面添加NavigationController美丽的NavBar)。将橙色标记为“CELL_ID”for reuse-identifier on Cell参数并为例如MyCell
你的故事板应该的样子(我已经改变细胞和标签的背景,所以它并不重要):
enter image description here

而这就是如何看您的自定义细胞类:

public partial class MyCell : UITableViewCell 
    { 
     public MyCell (IntPtr handle) : base (handle) 
     { 
     } 

     public void Update(string Data) 
     { 
      TxtLbl.Text = Data; 
     } 
    } 

TxtLbl - 是我们的标签位于细胞内的名称

进行额外的定制TableSource类,像这样的:

public class MySource : UITableViewSource 
    { 
     List<string> SourceData; 

     public MySource(List<string> _SourceData) 
     { 
      SourceData = _SourceData; 
     } 


     public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) 
     { 
      var Cell = tableView.DequeueReusableCell("CELL_ID") as MyCell; 

      Cell.Update(SourceData[indexPath.Row]); 

      return Cell; 
     } 

     public override nint RowsInSection(UITableView tableview, nint section) 
     { 
      return SourceData.Count; 
     } 

     public override void RowSelected(UITableView tableView, NSIndexPath indexPath) 
     { 
      tableView.DeselectRow(indexPath, true); 

     } 

     public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, Foundation.NSIndexPath indexPath) 
     { 
      switch (editingStyle) 
      { 
       case UITableViewCellEditingStyle.Delete: 
        Debug.WriteLine($"Source List until remove equals to {SourceData.Count} count"); 
        SourceData.RemoveAt(indexPath.Row); 
        tableView.DeleteRows(new NSIndexPath[] { indexPath }, UITableViewRowAnimation.Fade); 
        Debug.WriteLine($"Remove {indexPath.Row} index"); 
        Debug.WriteLine($"Source List after removing elements equals to {SourceData.Count}count"); 
        break; 
       case UITableViewCellEditingStyle.None: 
        Console.WriteLine("CommitEditingStyle:None called"); 
        break; 
      } 
     } 
     public override bool CanEditRow(UITableView tableView, NSIndexPath indexPath) 
     { 
      return true; // return false if you wish to disable editing for a specific indexPath or for all rows 
     } 

     public override string TitleForDeleteConfirmation(UITableView tableView, NSIndexPath indexPath) 
     { // Optional - default text is 'Delete' 
      return "Delete it!"; 
     } 
    } 

而对于你来说最有趣的部分是三种方法:CanEditRowTitleForDeleteConfirmationCommitEditingStyle

的ViewController内的方法viewDidLoad中()其中存储的TableView

var List = new List<string>(); 
      for (int i = 0; i < 100; i++) 
      { 
       List.Add($"My Item #{i.ToString()}"); 
      } 
      MyTable.Source = new MySource(List); 

,其结果是:
resultl

相关问题