Iphone 更改UIButton视图';新闻背景
这应该很容易,但它让我难堪。我在表格单元格的每一行上都有一个按钮。该按钮用于删除与该行关联的文件。我有一个图像,指示文件是否存在于iPhone上 当用户按下按钮时,目标操作方法(showDeleteSheet)然后调用一个带有变量的UIActionSheet,为其提供所按下行的indexPath。然后用户在操作表上按delete键,操作表的ClickedButtonIndex方法删除文件 现在,我需要更新按钮的背景图像属性,将相应表格单元格中的图像更改为未下载的图像。这可以通过按钮的目标操作方法或操作表的ClickedButtonIndex方法完成 下面是一些代码: 在表的CellForRowatineXpath方法中,我创建了按钮:Iphone 更改UIButton视图';新闻背景,iphone,uitableview,uibutton,Iphone,Uitableview,Uibutton,这应该很容易,但它让我难堪。我在表格单元格的每一行上都有一个按钮。该按钮用于删除与该行关联的文件。我有一个图像,指示文件是否存在于iPhone上 当用户按下按钮时,目标操作方法(showDeleteSheet)然后调用一个带有变量的UIActionSheet,为其提供所按下行的indexPath。然后用户在操作表上按delete键,操作表的ClickedButtonIndex方法删除文件 现在,我需要更新按钮的背景图像属性,将相应表格单元格中的图像更改为未下载的图像。这可以通过按钮的目标操作方法
UIButton *deleteButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[deleteButton addTarget:self action:@selector(showDeleteSheet:) forControlEvents:UIControlEventTouchDown];
deleteButton.frame = CGRectMake(246, 26, 30, 30);
if (![AppData isDownloaded:[dets objectForKey:@"fileName"]]) {
[deleteButton setBackgroundImage:notdownloadedImage forState:UIControlStateNormal];
} else {
[deleteButton setBackgroundImage:notdownloadedImage forState:UIControlStateNormal];
}
在按钮的目标方法中:
-(void)showDeleteSheet:(id)sender {
//get the cell row that the button is in
NSIndexPath *indexPath = [table indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSInteger currentSection = indexPath.section;
NSInteger currentIndexRow = indexPath.row;
NSLog(@"section = %i row = %i", currentSection, currentIndexRow);
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[table reloadData];
//if deleteButton is declared in .h then the other instances of deleteButton show 'local declaration hides instance variable'
[deleteButton setBackgroundImage:[UIImage imageNamed:@"not-downloaded.png"] forState:UIControlStateNormal];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Delete this file?"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Delete"
otherButtonTitles:nil];
actionSheet.tag = currentIndexRow;
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
[actionSheet release];
}
也许问题在于调用[deleteButton setBackgroundImage…];不知道应该更新哪个单元格的按钮。如果是这样的话,我不知道该怎么说
因为我测试的是,当按下按钮并设置背景图像时,文件是否已下载,当我向下滚动以使相关单元格取消排队时,然后向上滚动显示正确的图像
我试图强制重新加载表,但[表重新加载数据];他什么也没做。我尝试过重新加载RowsatindExpaths,但仍然没有效果
有人愿意教我如何做到这一点吗?您可以只存储来自
-(void)showDeleteSheet:(id)sender
实例变量中的方法
self.curButton = sender;
然后打电话
[curButton setBackgroundImage:image];
谢谢乔。。。这就是诀窍:[sender setBackgroundImage:[UIImage ImageName:@“not download arrow.png”]forState:UIControlStateNormal];现在。。。(总是有一个现在)。。。如果用户单击“取消”,图像仍会更改,因为图像更改发生在showDeleteSheet中,而不是在操作表的ClickedButtonIndex方法中。当然,我找不到寄件人。有什么想法吗?奇怪的是,我可以设置背景图像,但我不能将userInteractionEnabled设置为no,以禁用带有[sender userInteractionEnabled=no]的按钮;您的意思是sender.userInteractionEnabled=否;或[sender setUserInteractionEnabled:否];对吗?要解决取消问题,可以设置self.curButton=nil;当点击cancel按钮时,我现在明白了在实例变量中保存sender的意思。但是,我不知道如何使变量。。。无法将其设置为int,id也没有帮助。按钮的发送方看起来是一个数组。