我想創建自動處理文本字段填充的自定義表格單元格。我的想法是,我只是將一個對象傳遞給單元類,然後單元格將自動填充字段。一切工作正常,除了單元格內沒有按鈕工作,它們都是不可點擊的。我究竟做錯了什麼?自定義可重用UITableViewCell
主屏:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = [indexPath row];
Claim *claim = [statementsArray objectAtIndex:row];
NSString * strIndentifier;
strIndentifier = @"StatementDetailsCellIdentifier";
StatementDetailsCell *cell = (StatementDetailsCell *) [tableView dequeueReusableCellWithIdentifier:strIndentifier];
cell.hasWarranty = claim.hasWarranty;
if(cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"StatementDetailsCell" owner:self options:nil];
cell = [statementCell initWithClaim:claim reuseIdentifier:strIndentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
Cell.m:
-(id)initWithClaim:(Claim *)_claim reuseIdentifier:(NSString *)reuseIdentifier
{
self = [self initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
claim = _claim;
[self populate];
return self;
}
-(void)populate
{
barcodeLabel.text = claim.barcode;
NSLog(@"claim is %@", [claim description]);
if(claim.points == 0 || claim.points == 0.00)
valueLabel.text = @"Pending";
else
valueLabel.text = [NSString stringWithFormat:@"£%.2f", claim.points];
modelLabel.text = claim.product;
warrantyLabel.text = claim.warranty.name;
APIRequest *apiRequest = [[APIRequest alloc] init];
dateLabel.text = [apiRequest parseDate:claim.date];
//hasWarranty = claim.hasWarranty;
double timeS = [apiRequest getUnixTimestamp:claim.date];
NSDate *now = [NSDate date];
NSDate *trueDate = [NSDate dateWithTimeIntervalSince1970:timeS];
double timeDiffrece = [now timeIntervalSinceDate:trueDate];
double threemonths = 90*24*3600;
//Warranty Button
if(claim.hasWarranty)
{
UIImage *buttonImage;
//13-6-2013
if([claim.warranty.name isEqualToString:@"Pending"]) {
buttonImage = [UIImage imageNamed:@"imgBtnWarrantyPending.png"];
pendingHelp.hidden = NO;
} else {
pendingHelp.hidden = YES;
buttonImage = [UIImage imageNamed:@"warranty_claimed.png"];
}
[warrantyButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[warrantyButton setBackgroundImage:buttonImage forState:UIControlStateHighlighted];
[warrantyButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
}
else
{
pendingHelp.hidden = YES;
if(false) {
[warrantyButton setBackgroundImage:nil forState:UIControlStateNormal];
[warrantyButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
} else {
[warrantyButton setTag:claim.ID];
UIImage *buttonImage = [UIImage imageNamed:@"add_warranty.png"];
[warrantyButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[warrantyButton setBackgroundImage:nil forState:UIControlStateHighlighted];
[warrantyButton addTarget:self action:@selector(haha:) forControlEvents:UIControlEventTouchDown];
warrantyButton.userInteractionEnabled = YES;
warrantyButton.enabled = YES;
}
}
}
這是幹什麼用的? 'if(false){' – Tala
這只是一種繞過某些預期邏輯的臨時手段。 –
你有沒有初始化warrantyButton,否則所有的代碼將默默無法正常工作 – Tala