Iphone 按按钮时添加/删除图像

Iphone 按按钮时添加/删除图像,iphone,Iphone,这是我的tableview。我想当用户按下按钮时,另一个图像应该位于该图像的顶部,当用户再次按下wt时,该图像将被删除。这是代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CGRect prescriptionFrame=CGRectMake(100, 0, 150, 40); UILabel *presText

这是我的tableview。我想当用户按下按钮时,另一个图像应该位于该图像的顶部,当用户再次按下wt时,该图像将被删除。这是代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CGRect prescriptionFrame=CGRectMake(100, 0, 150, 40);
    UILabel *presTextLabel=[[UILabel alloc] initWithFrame:prescriptionFrame];
    presTextLabel.font=[UIFont boldSystemFontOfSize:20];

    CGRect tabletFrame=CGRectMake(150, 50, 100, 20);
    UILabel *tabletTextLabel=[[UILabel alloc] initWithFrame:tabletFrame];
    tabletTextLabel.font=[UIFont boldSystemFontOfSize:10];

    CGRect noOfTabletFrame=CGRectMake(250, 40, 30, 30);
    UILabel *noOfTabletTextLabel=[[UILabel alloc] initWithFrame:noOfTabletFrame];
    noOfTabletTextLabel.font=[UIFont boldSystemFontOfSize:10];

    cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cellButton.frame=CGRectMake(10,10 ,50,50);      


    presTextLabel.text=[appDelegate.prescriptionArray objectAtIndex:indexPath.row];
    tabletTextLabel.text=[appDelegate.tabletArray objectAtIndex:indexPath.row];
    noOfTabletTextLabel.text=[appDelegate.noOfTabletsArray objectAtIndex:indexPath.row];

    [cellButton setImage:[appDelegate.imageArray objectAtIndex:indexPath.row] 
forState:UIControlStateNormal];

    [cellButton addTarget:self action:@selector(StartTimer) 
forControlEvents:UIControlEventTouchUpInside];

    cellButton.tag = indexPath.row;
    [cell.contentView addSubview:presTextLabel];   
    [cell.contentView addSubview:tabletTextLabel];   
    [cell.contentView addSubview:noOfTabletTextLabel];    
    [cell.contentView addSubview:cellButton];

    [presTextLabel release];  
    [tabletTextLabel release];
    return cell;
}

未经测试,可能包括打字错误,但应能正常工作

制造商


侧扫

尝试更好地对代码进行分组。这不是真正可读的。当按下其他地方的按钮时,您试图更改每行的“cellButton”UIButton的图像,然后当再次按下该按钮时将其删除或更改回原来的图像??
- (void)StartTimer:(id)sender;
UIButton* button = (UIButton*)sender;
UITableViewCell *cell = (UITableViewCell*)[[button superview] superview];
NSIndexPath indexPath = [self.tableview indexPathForCell:cell];
if([button imageForState:UIControlStateNormal] == [appDelegate.imageArray objectAtIndex:indexPath.row]) {
    [button setImage:[UIImage imageNamed:@"NEWIMAGE.png"] forState:UIControlStateNormal];
}
else {
    [button setImage:[appDelegate.imageArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];

}