зеркало из https://github.com/xamarin/ios-samples.git
Fix bug #11612 in Shared resource.
Implement BtnSaveChangesTouchUpInside method to enable save contact function. Change some code in table data source to support phone number editing and saving.
This commit is contained in:
Родитель
6c78dff324
Коммит
46ad60aeb9
|
@ -151,7 +151,7 @@ namespace Example_SharedResources.Screens.iPhone.Contacts
|
||||||
ABMutableMultiValue<string> phones = contact.GetPhones ().ToMutableMultiValue ();
|
ABMutableMultiValue<string> phones = contact.GetPhones ().ToMutableMultiValue ();
|
||||||
|
|
||||||
// add the phone number to the phones via the multivalue.Add method
|
// add the phone number to the phones via the multivalue.Add method
|
||||||
phones.Add (new NSString (txtPhoneLabel.Text), new NSString (txtPhoneNumber.Text));
|
phones.Add (new NSString (txtPhoneNumber.Text), new NSString (txtPhoneLabel.Text));
|
||||||
|
|
||||||
// attach the phones back to the contact
|
// attach the phones back to the contact
|
||||||
contact.SetPhones (phones);
|
contact.SetPhones (phones);
|
||||||
|
@ -202,7 +202,44 @@ namespace Example_SharedResources.Screens.iPhone.Contacts
|
||||||
|
|
||||||
protected void BtnSaveChangesTouchUpInside (object sender, EventArgs e)
|
protected void BtnSaveChangesTouchUpInside (object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
|
using(ABAddressBook addressBook = new ABAddressBook ())
|
||||||
|
{
|
||||||
|
ABPerson contact = addressBook.GetPerson (contactID);
|
||||||
|
|
||||||
|
// save contact name information
|
||||||
|
contact.FirstName = txtFirstName.Text;
|
||||||
|
contact.LastName = txtLastName.Text;
|
||||||
|
|
||||||
|
// get the phones and copy them to a mutable set of multivalues (so we can edit)
|
||||||
|
ABMutableMultiValue<string> phones = contact.GetPhones ().ToMutableMultiValue ();
|
||||||
|
|
||||||
|
// remove all phones data
|
||||||
|
for (int i = phones.Count - 1; i >= 0 ; i--) {
|
||||||
|
phones.RemoveAt(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
// add the phone number to the phones from the table data source
|
||||||
|
for(int i=0; i<PhoneNumberTableSource.labels.Count; i++)
|
||||||
|
{
|
||||||
|
phones.Add (new NSString (PhoneNumberTableSource.numbers[i]), new NSString (PhoneNumberTableSource.labels[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
// attach the phones back to the contact
|
||||||
|
contact.SetPhones (phones);
|
||||||
|
|
||||||
|
// save the address book changes
|
||||||
|
addressBook.Save ();
|
||||||
|
|
||||||
|
// show an alert, letting the user know information saved successfully
|
||||||
|
new UIAlertView ("Alert", "Contact Information Saved!", null, "OK", null).Show();
|
||||||
|
|
||||||
|
// update the page
|
||||||
|
PopulatePage (contact);
|
||||||
|
|
||||||
|
// we have to call reload to refresh the table because the action didn't originate
|
||||||
|
// from the table.
|
||||||
|
tblPhoneNumbers.ReloadData ();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void PopulatePage (ABPerson contact)
|
protected void PopulatePage (ABPerson contact)
|
||||||
|
@ -215,6 +252,7 @@ namespace Example_SharedResources.Screens.iPhone.Contacts
|
||||||
txtLastName.Text = contact.LastName;
|
txtLastName.Text = contact.LastName;
|
||||||
tableDataSource = new AddressBookScreen.PhoneNumberTableSource (contact.GetPhones ());
|
tableDataSource = new AddressBookScreen.PhoneNumberTableSource (contact.GetPhones ());
|
||||||
tblPhoneNumbers.Source = tableDataSource;
|
tblPhoneNumbers.Source = tableDataSource;
|
||||||
|
|
||||||
|
|
||||||
// wire up our delete clicked handler
|
// wire up our delete clicked handler
|
||||||
tableDataSource.DeleteClicked +=
|
tableDataSource.DeleteClicked +=
|
||||||
|
@ -249,9 +287,27 @@ namespace Example_SharedResources.Screens.iPhone.Contacts
|
||||||
public event EventHandler<PhoneNumberClickedEventArgs> DeleteClicked;
|
public event EventHandler<PhoneNumberClickedEventArgs> DeleteClicked;
|
||||||
|
|
||||||
protected ABMultiValue<string> phoneNumbers { get; set; }
|
protected ABMultiValue<string> phoneNumbers { get; set; }
|
||||||
|
public static List<string> labels;
|
||||||
|
public static List<string> numbers;
|
||||||
|
|
||||||
|
|
||||||
public PhoneNumberTableSource(ABMultiValue<string> phoneNumbers)
|
public PhoneNumberTableSource(ABMultiValue<string> phoneNumbers)
|
||||||
{ this.phoneNumbers = phoneNumbers; }
|
{
|
||||||
|
this.phoneNumbers = phoneNumbers;
|
||||||
|
if(labels == null)
|
||||||
|
labels = new List<string>();
|
||||||
|
else
|
||||||
|
labels.Clear();
|
||||||
|
if(numbers == null)
|
||||||
|
numbers = new List<string>();
|
||||||
|
else
|
||||||
|
numbers.Clear();
|
||||||
|
for (int i=0; i<phoneNumbers.Count; i++)
|
||||||
|
{
|
||||||
|
labels.Add(phoneNumbers[i].Label.Description);
|
||||||
|
numbers.Add (phoneNumbers[i].Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public override int NumberOfSections (UITableView tableView) { return 1; }
|
public override int NumberOfSections (UITableView tableView) { return 1; }
|
||||||
|
|
||||||
|
@ -260,12 +316,28 @@ namespace Example_SharedResources.Screens.iPhone.Contacts
|
||||||
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
|
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
|
||||||
{
|
{
|
||||||
EditablePhoneTableCell cell = tableView.DequeueReusableCell ("PhoneCell") as EditablePhoneTableCell;
|
EditablePhoneTableCell cell = tableView.DequeueReusableCell ("PhoneCell") as EditablePhoneTableCell;
|
||||||
if(cell == null)
|
if (cell != null)
|
||||||
|
cell.dataIndex = indexPath.Row;
|
||||||
|
if (cell == null) {
|
||||||
cell = new EditablePhoneTableCell ("PhoneCell");
|
cell = new EditablePhoneTableCell ("PhoneCell");
|
||||||
|
cell.dataIndex = indexPath.Row;
|
||||||
cell.PhoneLabel = phoneNumbers[indexPath.Row].Label.ToString ().Replace ("_$!<", "").Replace (">!$_", "");
|
cell.txtLabel.EditingDidEnd += (sender, e) => {
|
||||||
cell.PhoneNumber = phoneNumbers[indexPath.Row].Value.ToString ();
|
labels[cell.dataIndex] = cell.PhoneLabel;
|
||||||
|
|
||||||
|
};
|
||||||
|
cell.txtPhoneNumber.EditingDidEnd += (sender, e) => {
|
||||||
|
numbers[cell.dataIndex] = cell.PhoneNumber;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
// cell.PhoneLabel = phoneNumbers[indexPath.Row].Label.ToString ().Replace ("_$!<", "").Replace (">!$_", "");
|
||||||
|
// cell.PhoneNumber = phoneNumbers[indexPath.Row].Value.ToString ();
|
||||||
|
cell.PhoneLabel = labels[indexPath.Row].Replace ("_$!<", "").Replace (">!$_", "");
|
||||||
|
cell.PhoneNumber = numbers [indexPath.Row];
|
||||||
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
|
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return cell;
|
return cell;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -298,13 +370,15 @@ namespace Example_SharedResources.Screens.iPhone.Contacts
|
||||||
protected class EditablePhoneTableCell : UITableViewCell
|
protected class EditablePhoneTableCell : UITableViewCell
|
||||||
{
|
{
|
||||||
// label and phone number text boxes
|
// label and phone number text boxes
|
||||||
UITextField txtLabel = new UITextField(new RectangleF(10, 5, 110, 33));
|
public UITextField txtLabel = new UITextField(new RectangleF(10, 5, 110, 33));
|
||||||
UITextField txtPhoneNumber = new UITextField(new RectangleF(130, 5, 140, 33));
|
public UITextField txtPhoneNumber = new UITextField(new RectangleF(130, 5, 140, 33));
|
||||||
|
|
||||||
// properties
|
// properties
|
||||||
public string PhoneLabel { get { return txtLabel.Text; } set { txtLabel.Text = value; } }
|
public string PhoneLabel { get { return txtLabel.Text; } set { txtLabel.Text = value; } }
|
||||||
public string PhoneNumber { get { return txtPhoneNumber.Text; } set { txtPhoneNumber.Text = value; } }
|
public string PhoneNumber { get { return txtPhoneNumber.Text; } set { txtPhoneNumber.Text = value; } }
|
||||||
|
|
||||||
|
public int dataIndex;
|
||||||
|
|
||||||
public EditablePhoneTableCell(string reuseIdentifier) : base(UITableViewCellStyle.Default, reuseIdentifier)
|
public EditablePhoneTableCell(string reuseIdentifier) : base(UITableViewCellStyle.Default, reuseIdentifier)
|
||||||
{
|
{
|
||||||
AddSubview(txtLabel);
|
AddSubview(txtLabel);
|
||||||
|
@ -316,7 +390,11 @@ namespace Example_SharedResources.Screens.iPhone.Contacts
|
||||||
txtPhoneNumber.ReturnKeyType= UIReturnKeyType.Done;
|
txtPhoneNumber.ReturnKeyType= UIReturnKeyType.Done;
|
||||||
txtPhoneNumber.BorderStyle = UITextBorderStyle.Line;
|
txtPhoneNumber.BorderStyle = UITextBorderStyle.Line;
|
||||||
txtPhoneNumber.ShouldReturn += (t) => { t.ResignFirstResponder(); return true; };
|
txtPhoneNumber.ShouldReturn += (t) => { t.ResignFirstResponder(); return true; };
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
Загрузка…
Ссылка в новой задаче