我想使用协议。我有一个集合视图和集合视图单元格属于此集合视图。我在这个单元格中有一个按钮。当用户点击此按钮时,我想在collectionview类(而不是单元类)中调用一个方法。
我正在使用以下代码定义单元类:
[HttpPost]
public HttpResponseMessage PostAccount([FromBody]FormDataCollection formData)
{
if (formData != null)
{
var nValueCol = formData.ReadAsNameValueCollection();
var account = new Account()
{
Email = nValueCol["email"],
Password = nValueCol["password"],
AgreedToTerms = Convert.ToBoolean(nValueCol["agreesToTerms"]),
//LocationAccountCreated = DbGeography.FromText(nValueCol["userLocation"])
};
var userProfile = new UserProfile()
{
FirstName = nValueCol["firstName"],
LastName = nValueCol["lastName"],
DateOfBirth = Convert.ToDateTime(nValueCol["dateOfBirth"])
};
var newAcc = _accountService.CreateAccount(account.Email, userProfile.FirstName, userProfile.LastName,
userProfile.DateOfBirth, account.Email, account.AgreedToTerms,
account.LocationAccountCreated);
var response = Request.CreateResponse(HttpStatusCode.Created);
return response;
}
else
return Request.CreateResponse(HttpStatusCode.NotAcceptable);
}
然后当用户点击按钮系统调用单元格类中的方法时。
protocol testPro {
func replyClicked()
}
class MessageGalleryViewControllerCellCollectionViewCell: UICollectionViewCell {
var test1: testPro?
// ...
}
我的集合视图类:
func replyAction(sender:UIButton!)
{
test1?.replyClicked()
}
但是我无法看到任何类似"点击"的日志。所以它不起作用。问题在哪里?
答案 0 :(得分:0)
您没有将test1
分配给您的手机。在cellForItemAtindexPath
中,在实例化单元格后放置cell.test1 = self
。