dea*_*ter 1 iphone objective-c uitableview ios
在我的应用程序中,我面临一个非常奇怪的问题.我试图解决它几个小时但到目前为止找不到问题.
好吧,我有自定义单元格的tableview.每个单元格我都有一个按钮.它就像一个空盒子.按下按钮时,我想更改该按钮的相关单元格图像.实际上我成功地做到了,但不知何故,其他一些细胞的按钮图像也在变化,我无法弄清楚为什么.这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//create new cell
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell.tickButton setTag:indexPath.row];
[cell.tickButton addTarget:self action:@selector(buttonWasPressed:)forControlEvents:UIControlEventTouchUpInside];
//cell has 2 label so I filled them with the contents of my object array that part is working with no problem
cell.leftLabel.text = [NSString stringWithFormat:@"%@", [[contacts objectAtIndex:indexPath.row]name]];
cell.rightLabel.text = [NSString stringWithFormat:@"%@", [[contacts objectAtIndex:indexPath.row]email]];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
在我的buttonWasPressed方法中,我只是这样做:
-(IBAction)buttonWasPressed:(id)sender
{
UIButton *button = (UIButton *)sender;
[button setImage:[UIImage imageNamed:@"ticck.jpeg"] forState:UIControlStateNormal];
}
Run Code Online (Sandbox Code Playgroud)
正如我之前说的那样有效,但也改变了一些其他按钮图像,当我向下滚动并回到初始位置时,我看到一些按钮图像被更改.
您可以访问该单元格,并可以将按钮图像更改为:
-(IBAction)buttonWasPressed:(id)sender
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:your_row inSection:your_section];
UITableViewCell *currentCell = [table cellForRowAtIndexPath:indexPath];
UIButton *button = (UIButton *)[currentCell viewWithTag:(UIButton *)sender.tag];
[button setImage:[UIImage imageNamed:@"ticck.jpeg"] forState:UIControlStateNormal];
}
Run Code Online (Sandbox Code Playgroud)
希望它能帮到你.
| 归档时间: |
|
| 查看次数: |
1598 次 |
| 最近记录: |