上周六我也在问类似的问题,但看起来聪明的人在烧烤派对上。没有得到答案。
我的问题是我需要在 UITableViewCell 上放置 UISwitch 和两个 UIButton,当 UISwitch 改变状态时,这些 UIButton 应该出现或消失。这些单元格不用于导航到任何地方,因此我仅将它们用于信息呈现,并且不使用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法中,我以编程方式创建了所需的控件,并为每个控件分配了标记值和操作:@selector。
现在在其他方法中,我管理了如何从当前行获取 itemID。就像在这个方法中一样:
-(void)setItem:(UISwitch*)sender
{
int n;
UITableViewCell *cell;
if(sender.tag==9001)
{
cell=((UITableViewCell*)[sender superview]);
n=([self.tableView indexPathForCell:cell].row);
}
item=[self.arr objectAtIndex:n];
NSDate *today=[NSDate date];
NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
NSString *now =[formatter stringFromDate:today];
if(sender.on)
{
DBAccess *access=[[DBAccess alloc]init];
[access setItemON:item.itemID date:now];
//qualityBtn.hidden=NO;
[qualityBtn setTitleColor:[UIColor blackColor] forState:normal];
[qualityBtn setTitle:@"UNDEF" forState:normal];
}
else
{
DBAccess *access=[[DBAccess alloc]init];
[access setItemOFF:item.itemID];
}
}
所以,我的问题是当这种方法有效时,其他单元格上的 qualityBtn:s 也会获得标题等,但我需要当前控件上的当前按钮会由于方法的逻辑而改变。那么,如何指出它只会更改放置在当前单元格上的控件而忽略其他控件的方法。
先感谢您。
编辑:
@property(nonatomic,strong)UIButton *qualityBtn; 是的,它是全球性的。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
item=[self.arr objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = @"Cell";
ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
self.switchView = [[UISwitch alloc] initWithFrame:CGRectMake(7, 8, 79, 27)];
[switchView addTarget:self action:@selector(setItem:) forControlEvents:UIControlEventValueChanged];
[switchView setTag:9001];
qualityBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[qualityBtn addTarget:self
action:@selector(setQuality:)
forControlEvents:UIControlEventTouchDown];
qualityBtn.frame = CGRectMake(236, 8, 72, 21);
qualityBtn.titleLabel.font = [UIFont systemFontOfSize:12];
[qualityBtn setTag:9002];
itemMemoBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[itemMemoBtn addTarget:self
action:@selector(setItemMemo:)
forControlEvents:UIControlEventTouchDown];
itemMemoBtn.frame = CGRectMake(274, 43, 34, 30);
[itemMemoBtn setTag:9003];
[itemMemoBtn setImage:[UIImage imageNamed:@"comment-icon"] forState:normal];
lblDate=[[UILabel alloc]initWithFrame:CGRectMake(14, 34, 72, 21)];
lblDate.font=[UIFont systemFontOfSize:9];
[lblDate setTag:9003];
[cell addSubview:switchView];
[cell addSubview:qualityBtn];
[cell addSubview:itemMemoBtn];
[cell addSubview:lblDate];
[cell setIndentationWidth:45];
[cell setIndentationLevel:1];
lblDate.text=item.date;
cell.lblKM.text=item.kmRef;
cell.lblInfo.text=item.info;
if(item.dateFrom.length>0 && item.dateUntil.length>0)
{
cell.lblYears.text=[NSString stringWithFormat:@"%@ - %@",item.dateFrom,item.dateUntil];
}
if(item.dateUntil.length<1)
{
cell.lblYears.text=item.dateFrom;
}
if(item.mintMark.length<1)
{
//cell.mintmarksImageView.hidden=YES;
}
cell.lblMintmarks.text=item.mintMark;
if(item.memo.length>0)
{
cell.lblMemoStatus.text=@"YES";
}
else
{
cell.lblMemoStatus.text=@"NO";
}
cell.lblRarity.text=item.rarity;
if(item.isAvailable==0)
{
[switchView setOn:NO];
//qualityBtn.hidden=YES;
//lblDate.hidden=YES;
qualityBtn.titleLabel.text=@"UNDEF";
}
else {
[switchView setOn:YES];
// lblDate.hidden=NO;
NSString *quality=nil;
switch(item.quality)
{
case 1:
{
quality=@"UNDEF";
break;
}
case 2:
{
quality=@"G";
break;
}
case 3:
{
quality=@"VG";
break;
}
case 4:
{
quality=@"F";
break;
}
case 5:
{
quality=@"VF";
break;
}
case 6:
{
quality=@"XF";
break;
}
case 7:
{
quality=@"UNC";
break;
}
case 8:
{
quality=@"PROOF";
break;
}
}
[qualityBtn setTitleColor:[UIColor blackColor] forState:normal];
[qualityBtn setTitle:quality forState:normal];
}
return cell;
}