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

时间:2011-08-13 10:51:26

标签: iphone

这是我的tableview.i想要当用户按下按钮然后另一个图像应该在该图像的顶部并且当用户按下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;
}

1 个答案:

答案 0 :(得分:0)

- (void)StartTimer:(id)sender;

DO

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];

}

未经测试,可能包括拼写错误,但应该有效。

MFG,

SideSwipe