【发布时间】:2014-10-30 10:04:22
【问题描述】:
我正在开发一个应用程序,我需要声明一个像全局变量一样的字符串数组以在某些方法中使用它,但问题是我无法分配它。
我的代码:
protected class ServiceTableSource : UITableViewSource
{
**string first = null;
string second = null;
protected string[] uuids;**
protected const string cellID = "BleServiceCell";
public event EventHandler<ServiceSelectedEventArgs> ServiceSelected = delegate {};
public List<CBService> Services {
get { return this._services; }
set { this._services = value; }
}
protected List<CBService> _services = new List<CBService> ();
public override int NumberOfSections (UITableView tableView)
{
return 1;
}
**public override int RowsInSection (UITableView tableview, int section)
{
return uuids.Length; <---- HERE'S THE PROBLEM!!!!
}**
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell (cellID);
**CBService service = this._services [indexPath.Row];** <--HERE'S ANOTHER PROBLEM!!!! I CAN`T REMOVE THE [INDEXPATH.ROW] BECOUSE IS A LIST.
if (cell == null) {
cell = new UITableViewCell (UITableViewCellStyle.Subtitle, cellID);
}
**first = service.UUID.ToString();
second = service.Peripheral.Identifier.ToString();
uuids = new string[]{first, second};**
cell.DetailTextLabel.Text = "UUID:" + uuids[indexPath.Row];
return cell;
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
**CBService service = this._services [indexPath.Row];** HERE'S ANOTHER PROBLEM!!!!
this.ServiceSelected (this, new ServiceSelectedEventArgs (service));
tableView.DeselectRow (indexPath, true);
}
public class ServiceSelectedEventArgs : EventArgs
{
public CBService Service {
get { return this._service; }
set { this._service = value; }
}
protected CBService _service;
public ServiceSelectedEventArgs (CBService service)
{
this._service = service;
}
}
}
我需要使用“uuids.Lenght”来获取行数并在方法 NumberOfSections 上使用。如果有人有其他可行的想法,我会同意kkk
我无法定义变量 first、second 或 uuids 的值,因为我的值是 services.uuid 和 services.peripheral.uuid,我无法声明服务
掠夺代码中的 cmets。
【问题讨论】:
-
你的意思是静态变量吗?一个在类的所有实例中都相同的变量?
-
是的,我试试,但我需要 GetCell 方法中的变量 NSIndexPath 的值,如果我将其声明为静态,则该值将为“null”
标签: c# ios uitableview variables xamarin