我解决了这个问题如下:
这是我对 UITableViewController 类的实现。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.delegate = self;
[cells addObject:cell];
// Configure the cell...
return cell;
}
- (void)mustDeselectWithout:(CustomCell *)cell{
for (CustomCell * currentCell in cells) {
if(currentCell != cell){
[currentCell deselect];
}
}
}
如您所见,当我创建单元格时,我在方法 cellForRowAtIndexPath 中返回自定义单元格。
这是自定义单元格的类
。H
#import <UIKit/UIKit.h>
#import "ProtocolCell.h"
@interface CustomCell : UITableViewCell {
UIButton * button;
}
@property(nonatomic, assign) id<ProtocolCell> delegate;
- (void)select;
- (void)deselect;
@end
和.m
#import "CustomCell.h"
@implementation CustomCell
@synthesize delegate;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(5, 5, 25, 25)];
[button setBackgroundColor:[UIColor blueColor]];
[button addTarget:self action:@selector(select) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:button];
}
return self;
}
- (void)select{
[button setBackgroundColor:[UIColor redColor]];
[delegate mustDeselectWithout:self];
}
- (void)deselect{
[button setBackgroundColor:[UIColor blueColor]];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc
{
[super dealloc];
}
@end
我的类 UITableViewController 实现协议ProtocolCell。当我点击按钮时,我的委托调用方法mustDeselectWithout并取消选择我的数组中没有当前按钮的所有按钮。
#import <Foundation/Foundation.h>
@class CustomCell;
@protocol ProtocolCell <NSObject>
- (void)mustDeselectWithout:(CustomCell *)cell;
@end
这是我的实现。如果你能发表意见,我会很高兴。因为我不知道这是否是我的问题的正确解决方案。谢谢!