当用户点击 tableview 上的 UIButton
(tableview 每行有多个按钮)时,会显示另一个 View 。
我想在用户单击另一个 UIView 的完成按钮后更改此按钮的图像。我怎样才能做到这一点?我是新手。你能为我提供一些代码吗?提前致谢。
更新代码:
表格 View 代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"%d,%d",indexPath.section,indexPath.row];
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[market setTag:3000];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[cell.contentView addSubview:market];
}
for (UIButton *button in [cell subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
button.tag = indexPath.row;
[button setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal];
}
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
return cell;
}
打开按钮代码(用户点击显示另一个 View 的按钮)
- (void)marketPressedAction:(id)sender
{
UIButton *button = (UIButton *)sender;
buttontag = button.tag;
NSLog(@"Market button click at row %d",buttontag);
}
完成按钮的代码:
-(void)submitMarket
{
for (UIButton *button in [_tableView subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
if (button.tag == buttontag) {
[button setBackgroundImage:[UIImage imageNamed:@"MarketplaceSelect.png"] forState:UIControlStateNormal];
}
}
}
}
最佳答案
试试这个:
[yourButton setBackgroundImage:someImge forState:UIControlStateNormal];
点击按钮的代码:
savedTag = button.tag;
点击完成按钮的代码:
for (UIButton *button in [table subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
if (button.tag == savedtag) {
[button setBackgroundImage:someImge forState:UIControlStateNormal];
}
}
}
在 cellForRowAtIndexPath 中代替此:[market setTag:3000];
写入 [market setTag:indexPath.row];
试试这个:
替换:
marketButton = (UIButton *)[cell.contentView viewWithTag:3000];
[marketButton setTag:indexPath.row];
有了这个:
for (UIButton *button in [cell subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
button.tag == indexPath.row;
}
}
还有一件事:将 cellForRowAtIndexPath 的第一行设为:
NSString *CellIdentifier = @"tableCell";
将您的 cellForRowAtIndexPath 更改为:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[market setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal];
[market setTag:indexPath.row];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[cell.contentView addSubview:market];
}
else {
for (UIButton *button in [cell subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
button.tag = indexPath.row;
}
}
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
return cell;
}
关于ios - 用户单击完成按钮后如何更改 UIButton 的图像是 iOS 中的另一个 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18247704/