2011-08-13 196 views
0

當用戶按下按鈕,然後另一個圖像應該對圖像的頂部和刪除這是我tableview.i想再次當用戶按重量是代碼添加/刪除按鈕按下圖像

- (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; 
} 
+0

嘗試將你的代碼段更好。這是不是真的可讀.. –

+0

你試圖改變每一行的'cellButton'UIButton的圖像,當一個按鈕在其他地方被按下,然後再次按下該按鈕時刪除或改回它? – mattacular

回答

0

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

} 

沒有測試,包括錯別字,但應該工作。

MFG,

側擊