专家,
我有一个UITableView,它填充了JSON文件中的一些单元格。我希望用户滑动以删除特定的单元格。我使用SWTableViewCell组件实现了删除功能,几乎让它处理了一个问题:"无效更新:第0和第34节中的行数无效。我怀疑我需要从我的模型中的数据源和我的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 :(得分:1)
好的,让我们试试一个简单的例子。
制作 ViewController 并放在那里 TableView (另外,您可以在顶部添加 NavigationController 以获得漂亮的 NavBar ) 。把橙色给予&#34; CELL_ID &#34;对于Cell参数的重用标识符,并为例如新的类创建的了myCell 即可。
你的故事板应该是这样的(我改变了Cell和Label的背景,所以它并不重要):
那就是如何看待你的自定义 Cell 类:
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!";
}
}
最有趣的部分是三种方法: CanEditRow , TitleForDeleteConfirmation , CommitEditingStyle 。
ViewDidLoad() ViewView 中 ViewController 内 TableView :var List = new List<string>();
for (int i = 0; i < 100; i++)
{
List.Add($"My Item #{i.ToString()}");
}
MyTable.Source = new MySource(List);
结果是: