Uitableview 查找UIPickerView选择的值,我将其添加为UIAlertController内的子视图
我有UITableViewController,在里面,我在单元格中添加了一个按钮。 单击该按钮后,将显示带有“确定”和“取消”操作的UIAlertController 在我的UIAlertController中,我添加了一个带有一些选项的UIPickerView 当用户单击单元格中的按钮时,UIAlertController弹出并显示带有“确定”和“取消”操作的UIPickerView inside AlertBox 单击UIAlertController的“确定”按钮后,您可以帮助我查找UIPickerView选择的值吗Uitableview 查找UIPickerView选择的值,我将其添加为UIAlertController内的子视图,uitableview,xamarin,xamarin.ios,uipickerview,uialertcontroller,Uitableview,Xamarin,Xamarin.ios,Uipickerview,Uialertcontroller,我有UITableViewController,在里面,我在单元格中添加了一个按钮。 单击该按钮后,将显示带有“确定”和“取消”操作的UIAlertController 在我的UIAlertController中,我添加了一个带有一些选项的UIPickerView 当用户单击单元格中的按钮时,UIAlertController弹出并显示带有“确定”和“取消”操作的UIPickerView inside AlertBox 单击UIAlertController的“确定”按钮后,您可以帮助我查找UI
namespace Navigation
{
public partial class ReviewInspectionViewController : UIViewController
{
private string filePath;
private InspectionData inspData = null;
List<ReportObservation> lstCriticalObs = new List<ReportObservation>();
List<ReportObservation> lstNonCriticalObs = new List<ReportObservation>();
public Audits Audit
{
get
{
return this._audit;
}
set
{
this._audit = value;
}
}
public ReviewInspectionViewController(IntPtr handle) : base(handle)
{
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
filePath = InProgressPath + Audit.AssessmentId.ToString() + ".xml";
inspData = ReadInspectionData(filePath);
if (inspData != null)
{
for (int i = 0; i < inspData.ReportSections.Count; i++)
{
lstCriticalObs.AddRange(inspData.ReportSections[i].ReportObservations.FindAll(x => x.Critical == true));
}
for (int i = 0; i < inspData.ReportSections.Count; i++)
{
lstNonCriticalObs.AddRange(inspData.ReportSections[i].ReportObservations.FindAll(x => x.Critical == false));
}
tblReviewInspection.Source = new ReviewInspectionSource(this, inspData, lstCriticalObs, lstNonCriticalObs);
tblReviewInspection.RowHeight = 160.0f;
}
}
}
class ReviewInspectionSource : UITableViewSource
{
NSString _cellID = new NSString("TableCell");
ReviewInspectionViewController _parent;
private InspectionData _inspectionData;
private List<ReportObservation> _lstCriticalObs;
private List<ReportObservation> _lstNonCriticalObs;
UITableView tvStatus;
public ReviewInspectionSource(ReviewInspectionViewController parent,
InspectionData inspectionData,
List<ReportObservation> lstCriticalObs,
List<ReportObservation> lstNonCriticalObs)
{
_inspectionData = inspectionData;
_lstCriticalObs = lstCriticalObs;
_lstNonCriticalObs = lstNonCriticalObs;
_parent = parent;
}
public override nint NumberOfSections(UITableView tableView)
{
return _lstCriticalObs.Count;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
int cnt = _lstCriticalObs[Convert.ToInt32(section)].ReportFindings.Count;
return cnt;
}
public override UIView GetViewForHeader(UITableView tableView, nint section)
{
UIView ctrls = new UIView();
tvStatus = new UITableView();
UILabel headerLabel = new UILabel(new CGRect(55, 4, 350, 33)); // Set the frame size you need
headerLabel.TextColor = UIColor.Red; // Set your color
headerLabel.Text = _lstCriticalObs[Convert.ToInt32(section)].ObsTitle;
UIButton btnEdit = new UIButton(new CGRect(5, 4, 50, 33));
var newFont = UIFont.SystemFontOfSize(17);
btnEdit.SetTitle("Edit", UIControlState.Normal);
btnEdit.Font = newFont;
btnEdit.BackgroundColor = UIColor.Yellow;
btnEdit.SetTitleColor(UIColor.Black, UIControlState.Normal);
btnEdit.SetTitleColor(UIColor.Black, UIControlState.Selected);
btnEdit.TouchUpInside += (object sender, EventArgs e) =>
{
tvStatus.Frame = new CGRect(((UIButton)sender).Frame.X, ((UIButton)sender).Frame.Y, 50, 230);
tvStatus.Hidden = false;
btnEdit.Hidden = true;
//string msg = "SectionIndex: " + section;
var Alert = UIAlertController.Create("Inspection", "\n\n\n\n\n\n", UIAlertControllerStyle.Alert);
Alert.ModalInPopover = true;
var picker = new UIPickerView(new CGRect(5, 20, 250, 140)) ;
picker.Model = new ObsStatusModel();
Alert.View.AddSubview(picker);
Alert.AddTextField((field) => {
field.Placeholder = "email address";
});
Alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, (action) => {
}));
Alert.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, null ));
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(Alert, true, null);
/*
@IBAction func showChoices(_ sender: Any) {
let alert = UIAlertController(title: "Car Choices", message: "\n\n\n\n\n\n", preferredStyle: .alert)
alert.isModalInPopover = true
let pickerFrame = UIPickerView(frame: CGRect(x: 5, y: 20, width: 250, height: 140))
alert.view.addSubview(pickerFrame)
pickerFrame.dataSource = self
pickerFrame.delegate = self
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (UIAlertAction) in
print("You selected " + self.typeValue )
}))
self.present(alert,animated: true, completion: nil )
}
*/
};
List<string> lstStatus = new List<string>();
lstStatus.Add(" "); lstStatus.Add("IN"); lstStatus.Add("OUT"); lstStatus.Add("N/A"); lstStatus.Add("N/O");
tvStatus.Source = new StatusSource(lstStatus);
tvStatus.Layer.BorderColor = new CGColor(0, 0, 0);
tvStatus.Layer.BorderWidth = 2;
tvStatus.BackgroundColor = UIColor.White;
tvStatus.Hidden = true;
// tvStatus.Layer.ZPosition = 1;
tvStatus.ResignFirstResponder();
// btnEdit.BringSubviewToFront(tvStatus);
// btnEdit.Layer.ZPosition = 0;
//UILabel editLabel = new UILabel(new CGRect(360, 4, 100, 33)); // Set the frame size you need
//editLabel.TextColor = UIColor.Green; // Set your color
//editLabel.Text = "Edit Label";
ctrls.AddSubviews(headerLabel, btnEdit, tvStatus);
return ctrls;
}
public override nfloat GetHeightForHeader(UITableView tableView, nint section)
{
nfloat height = 50.0f;
return height;
}
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
string msg = "Selected Row, ReviewInspection -- SectionIndex: " + indexPath.Section.ToString() + " RowIndex: " + indexPath.Row;
tvStatus.Hidden = true;
tvStatus.ResignFirstResponder();
//var Alert = UIAlertController.Create("Inspection", msg, UIAlertControllerStyle.Alert);
//Alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
//UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(Alert, true, null);
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
ReportFinding currentRequirementItem = _lstCriticalObs[indexPath.Section].ReportFindings[indexPath.Row]; // _inspectionData.ReportSections[indexPath.Row];
var cell = tableView.DequeueReusableCell(_cellID) as ReviewInspectionTableCell;
if (cell == null) { cell = new ReviewInspectionTableCell(_cellID); }
UIView customColorView = new UIView();
customColorView.BackgroundColor = UIColor.FromRGB(39, 159, 218);
UIView SelectedRowcustomColorView = new UIView();
SelectedRowcustomColorView.BackgroundColor = UIColor.FromRGB(255, 255, 255);
UIColor clrChecklistButton = UIColor.White;
UIColor clrFont = UIColor.White;
cell.TextLabel.LineBreakMode = UILineBreakMode.WordWrap;
cell.PreservesSuperviewLayoutMargins = false;
cell.LayoutMargins = UIEdgeInsets.Zero;
cell.BackgroundView = customColorView;
cell.SelectedBackgroundView = SelectedRowcustomColorView;
cell.Edit.TouchUpInside += (object sender, EventArgs e) =>
{
string msg = "SectionIndex: " + indexPath.Section.ToString() + " RowIndex: " + indexPath.Row;
var Alert = UIAlertController.Create("Inspection", msg, UIAlertControllerStyle.Alert);
Alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(Alert, true, null);
};
cell.Delete.TouchUpInside += (object sender, EventArgs e) =>
{
string msg = "SectionIndex: " + indexPath.Section.ToString() + " RowIndex: " + indexPath.Row;
var Alert = UIAlertController.Create("Inspection", msg, UIAlertControllerStyle.Alert);
Alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(Alert, true, null);
};
cell.UpdateCellControlsWithAuditData(currentRequirementItem.ViolationText,
currentRequirementItem.FindingAnswer,
currentRequirementItem.RepeatViolation,
clrChecklistButton,
clrFont);
return cell;
}
}
public class ReviewInspectionTableCell : UITableViewCell
{
UILabel violationText, findingAnswer, repeatViolation;
UIButton btnEdit, btnDelete;
public ReviewInspectionTableCell(NSString cellId) : base(UITableViewCellStyle.Default, cellId)
{
SelectionStyle = UITableViewCellSelectionStyle.Gray;
ContentView.BackgroundColor = UIColor.FromRGB(110, 214, 254);
ContentView.Layer.CornerRadius = 8;
var newFont = UIFont.SystemFontOfSize(17);
violationText = new UILabel();
violationText.Font = newFont;
violationText.LineBreakMode = UILineBreakMode.WordWrap;
violationText.TextAlignment = UITextAlignment.Left;
violationText.Lines = 0;
violationText.TextColor = UIColor.White;
violationText.HighlightedTextColor = UIColor.Green;
violationText.BackgroundColor = UIColor.Gray;
findingAnswer = new UILabel();
findingAnswer.Font = newFont;
findingAnswer.BackgroundColor = UIColor.Orange;
findingAnswer.LineBreakMode = UILineBreakMode.WordWrap;
findingAnswer.TextAlignment = UITextAlignment.Left;
findingAnswer.Lines = 0;
findingAnswer.TextColor = UIColor.White;
repeatViolation = new UILabel();
repeatViolation.Font = newFont;
repeatViolation.BackgroundColor = UIColor.Purple;
btnEdit = new UIButton();
btnEdit.Font = newFont;
btnEdit.SetTitle("Edit", UIControlState.Normal);
btnEdit.SetTitleColor(UIColor.Black, UIControlState.Selected);
btnDelete = new UIButton();
btnDelete.Font = newFont;
btnDelete.SetTitle("Delete", UIControlState.Normal);
btnDelete.SetTitleColor(UIColor.Black, UIControlState.Selected);
ContentView.AddSubviews(new UIView[] { violationText, findingAnswer, repeatViolation, btnEdit, btnDelete });
}
public UIButton Delete
{
get
{
return btnDelete;
}
}
public UIButton Edit
{
get
{
return btnEdit;
}
}
public void UpdateCellControlsWithAuditData(string _violationText,
string _findingAnswer,
Boolean _repeatVilation,
UIColor btnColor,
UIColor clrFont)
{
string repeat = string.Empty;
if (_repeatVilation)
{
repeat = "Repeat Violation.";
}
violationText.Text = _violationText;
findingAnswer.Text = _findingAnswer;
repeatViolation.Text = repeat;
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
//x, y, width, height
violationText.Frame = new CGRect(50, 0, 700, 80);
findingAnswer.Frame = new CGRect(50, 81, 700, 33);
repeatViolation.Frame = new CGRect(50, 110, 350, 33);
btnEdit.Frame = new CGRect(5, 0, 50, 33);
btnEdit.Layer.CornerRadius = 8;
btnEdit.BackgroundColor = UIColor.FromRGB(19, 120, 170);
btnDelete.Frame = new CGRect(5, 34, 50, 33);
btnDelete.Layer.CornerRadius = 8;
btnDelete.BackgroundColor = UIColor.FromRGB(19, 120, 170);
}
}
public class ObsStatusModel : UIPickerViewModel
{
static string[] names = new string[] {
"Monitor",
"Comprehensive",
"OutBreak Investigation",
"Complaint",
"FollowUp",
"PlanReview",
"Other"
};
public ObsStatusModel()
{
}
public override nint GetComponentCount(UIPickerView v)
{
return 1;
}
public override nint GetRowsInComponent(UIPickerView pickerView, nint component)
{
return names.Length;
}
public override string GetTitle(UIPickerView picker, nint row, nint component)
{
return names[row];
}
}
class StatusSource : UITableViewSource
{
NSString _cellID = new NSString("TableCell");
private List<string> _lstStatus;
public StatusSource(List<string> lstStatus)
{
_lstStatus = lstStatus;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return _lstStatus.Count;
}
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
var Alert = UIAlertController.Create("Inspection", string.Empty, UIAlertControllerStyle.Alert);
Alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(Alert, true, null);
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
string currentStatusItem = _lstStatus[indexPath.Row];
var cell = tableView.DequeueReusableCell(_cellID) as StatusTableCell;
if (cell == null) { cell = new StatusTableCell(_cellID); }
cell.TextLabel.LineBreakMode = UILineBreakMode.WordWrap;
cell.PreservesSuperviewLayoutMargins = false;
cell.LayoutMargins = UIEdgeInsets.Zero;
cell.UpdateCellControlsWithAuditData(currentStatusItem);
return cell;
}
}
public class StatusTableCell : UITableViewCell
{
UILabel statusText;
public StatusTableCell(NSString cellId) : base(UITableViewCellStyle.Default, cellId)
{
SelectionStyle = UITableViewCellSelectionStyle.Gray;
//ContentView.BackgroundColor = UIColor.FromRGB(110, 214, 254);
//ContentView.Layer.CornerRadius = 8;
var newFont = UIFont.SystemFontOfSize(17);
statusText = new UILabel();
statusText.Font = newFont;
statusText.LineBreakMode = UILineBreakMode.WordWrap;
statusText.TextAlignment = UITextAlignment.Center;
statusText.Lines = 0;
statusText.TextColor = UIColor.Black;
statusText.HighlightedTextColor = UIColor.Green;
statusText.BackgroundColor = UIColor.White;
ContentView.AddSubviews(new UIView[] { statusText });
}
public void UpdateCellControlsWithAuditData(string status)
{
statusText.Text = status;
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
//x, y, width, height
statusText.Frame = new CGRect(5, 5, 100, 25);
}
}
名称空间导航
{
公共部分类ReviewInspectionViewController:UIViewController
{
私有字符串文件路径;
私有检查数据inspData=null;
List lstCriticalObs=新列表();
List lstNonCriticalObs=新列表();
公共审计
{
得到
{
返回此文件。\u审计;
}
设置
{
这是审计=价值;
}
}
public ReviewInspectionViewController(IntPtr句柄):基本(句柄)
{
}
公共覆盖无效视图将出现(布尔动画)
{
基本视图将显示(动画);
}
公共覆盖无效ViewDidLoad()
{
base.ViewDidLoad();
filePath=InProgressPath+Audit.AssessmentId.ToString()+“.xml”;
inspData=ReadInspectionData(文件路径);
如果(inspData!=null)
{
对于(int i=0;ix.Critical==true));
}
对于(int i=0;ix.Critical==false));
}
tblReviewInspection.Source=新的ReviewInspectionSource(此、更新数据、lstCriticalObs、lstNonCriticalObs);
tblReviewInspection.RowHeight=160.0f;
}
}
}
类ReviewInspectionSource:UITableViewSource
{
NSString _cellID=新的NSString(“表格单元格”);
查看检查视图控制器\u父级;
私有检查数据_检查数据;
私人列表;
私有列表lstunoncriticalobs;
UITableView状态;
公共ReviewInspectionSource(ReviewInspectionViewController父级,
检查数据检查数据,
列出关键OB,
列表(非关键OBS)
{
_检查数据=检查数据;
_lstCriticalObs=lstCriticalObs;
_lstNonCriticalObs=lstNonCriticalObs;
_父母=父母;
}
公共覆盖Nin NumberOfSections(UITableView tableView)
{
返回lstCriticalObs.Count;
}
公共覆盖第九行第九节(UITableView表格视图,第九节)
{
int cnt=_lstCriticalObs[Convert.ToInt32(section)].ReportFindings.Count;
返回cnt;
}
公共覆盖UIView GetViewForHeader(UITableView表格视图,第九节)
{
UIView ctrls=新UIView();
tvStatus=新UITableView();
UILabel headerLabel=new UILabel(new CGRect(55,4350,33));//设置所需的帧大小
headerLabel.TextColor=UIColor.Red;//设置颜色
headerLabel.Text=lstCriticalObs[Convert.ToInt32(section)].ObsTitle;
UIButton btnEdit=新的UIButton(新的CGRect(5,4,50,33));
var newFont=UIFont.SystemFontOfSize(17);
btnEdit.SetTitle(“编辑”,UIControlState.Normal);
Font=newFont;
btnEdit.BackgroundColor=UIColor.Yellow;
btnEdit.SetTitleColor(UIColor.Black,UIControlState.Normal);
btnEdit.SetTitleColor(UIColor.Black,UIControlState.Selected);
btnEdit.TouchUpInside+=(对象发送方,事件参数e)=>
{
tvStatus.Frame=新的CGRect(((UIButton)发送器).Frame.X,((UIButton)发送器).Frame.Y,50230);
tvStatus.Hidden=false;
btnEdit.Hidden=true;
//字符串msg=“SectionIndex:”+节;
var Alert=UIAlertController.Create(“检查”,“\n\n\n\n\n”,UIAlertControllerStyle.Alert);
Alert.ModalInPopover=true;
var picker=new-UIPickerView(new-CGRect(5,20250140));
picker.Model=new obstatusmodel();
Alert.View.AddSubview(选择器);
Alert.AddTextField((字段)=>{
field.Placeholder=“电子邮件地址”;
});
Alert.AddAction(UIAlertAction.Create(“确定”),UIAlertActionStyle.Default,(action)=>{
}));
AddAction(UIAlertAction.Create(“取消”,UIAlertActionStyle.Cancel,null));
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(警报、真、空);
/*
@iAction func showChoices(u发件人:任意){
let alert=UIAlertController(标题:“车辆选择”,消息:“\n\n\n\n\n”,首选样式:。警报)
alert.isModalInPopover=true
让pickerFrame=UIPickerView(帧:CGRect(x:5,y:20,宽度:250,高度:140))
alert.view.addSubview(pickerFrame)
pickerFrame.dataSource=self
pickerFrame.delegate=self
addAction(UIAlertAction(标题:“取消”,样式:。取消,处理程序:nil))
addAction(UIAlertAction)(标题:“确定”,样式:。默认,处理程序:{(UIAlertAction)在
打印(“您选择的”+self.typeValue)
}))
self.present(警报、动画:true、完成:nil)
}
*/
};
List lstStatus=新列表();
lstStatus.Add(“”);lstStatus.Add(“IN”);lstStatus.Add(“OUT”);lstStatus.Add(“不适用”);lstStatus.Add(“不适用”);
tvStatus.Source=新状态
public void CreatAlertView()
{
UIAlertController alert = UIAlertController.Create("Inspection\n\n\n\n\n\n\n", "", UIAlertControllerStyle.Alert);
alert.AddTextField((UITextField obj) =>
{
obj.Placeholder = "email address";
});
UIAlertAction OKAction = UIAlertAction.Create("OK", UIAlertActionStyle.Default, (obj) =>
{
if(alert.TextFields[0].Text.Length!=0)
{
//do some thing
}
});
UIAlertAction CancelAction = UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, null);
alert.AddAction(OKAction);
alert.AddAction(CancelAction);
var picker = new UIPickerView(new CGRect(0, 0,270,220));
picker.WeakDelegate = this;
picker.DataSource = this;
this.pickerView = picker;
alert.View.AddSubview(pickerView);
alertController = alert;
this.PresentViewController(alertController, false, null);
}
public nint GetComponentCount(UIPickerView pickerView)
{
return 1;
}
public nint GetRowsInComponent(UIPickerView pickerView, nint component)
{
if (component==0)
{
return (System.nint)itemsArray.Length;
}
else
{
return 0;
}
}
[Export("pickerView:titleForRow:forComponent:")]
public string GetTitle(UIPickerView pickerView, nint row, nint component)
{
string content = itemsArray[(nuint)row].ToString();
return content;
}
[Export("pickerView:didSelectRow:inComponent:")]
public void Selected(UIPickerView pickerView, nint row, nint component)
{
string content= itemsArray[(nuint)row].ToString();
//do some thing
}
this.itemsArray= new string[] {"apple","google","apple","google","apple","google"} ;//init the source array
CreatAlertView();