在customCell中我创建了一些UIButtons和[self addSubview:button]; 在UITableView [button addTarget:action:forControlEvents:]中,当我单击单元格indexPath.row = 0中的按钮时,单击了indexPath.row = 11; 如何点击按钮和其他按钮不受影响?
enter code here
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[SettingTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier]autorelease];
}
if (indexPath.section == 0) {
[cell.customBtn setTitle:@"" forState:UIControlStateNormal];
}
else if(indexPath.section == 1){
if (indexPath.row == 0) {
[cell.customBtn setTitle:@"" forState:UIControlStateNormal];
}
else if(indexPath.row == 1){
[cell.customBtn setTitle:@"" forState:UIControlStateNormal];
}
else{
[cell.customBtn setTitle:@"" forState:UIControlStateNormal];
}
else if (indexPath.section == 2){
if (indexPath.row == 0) {
[cell.customBtn setTitle:@"" forState:UIControlStateNormal];
}
else {
[cell.customBtn setTitle:@"" forState:UIControlStateNormal];
}
}
[cell.WIFIButton addTarget:self action:@selector(openOrCloseButtonClick:)
forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 7;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 1) {
return 3;
}
else if(section == 2){
return 2;
}
else if(section == 6){
return 4;
}
else{
return 1;
}
}
enter code here
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
CGFloat originX = IsIOS7?10:15;
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(originX, 7, 150, 30)];
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.font = [UIFont systemFontOfSize:16];
nameLabel.textColor = [UIColor blackColor];
self.nameLabel = nameLabel;
[self addSubview:nameLabel];
[nameLabel release];
CGFloat rightX = IsIOS7?180:160;
UILabel *fileSizeLabel = [[UILabel alloc]initWithFrame:CGRectMake(rightX - 10, 10, 100,
25)];
fileSizeLabel.backgroundColor = [UIColor clearColor];
fileSizeLabel.font = [UIFont systemFontOfSize:14];
fileSizeLabel.hidden = YES;
self.fileSizeLabel = fileSizeLabel;
[self addSubview:fileSizeLabel];
[fileSizeLabel release];
//This is customButton
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(rightX, 10, 50, 25);
button.backgroundColor = [UIColor grayColor];
self.WIFIButton = button;
[self addSubview:button];
}
答案 0 :(得分:0)
在代码上稍微跳一下枪,但请考虑以下内容。添加一种方法,可以在不使用标签的情况下查找显示视图的单元格。
- (NSIndexPath *)indexPathOfSubview:(UIView *)view {
while (view && ![view isKindOfClass:[UITableViewCell self]]) {
view = view.superview;
}
UITableViewCell *cell = (UITableViewCell *)view;
return [self.tableView indexPathForCell:cell];
}
这假设您有一个名为tableView的插座指向一个表(它假定该参数是一个表的单元格的子视图)。现在,按下按钮时运行的任何方法:
- (IBAction)myButtonPressed:(id)sender {
NSIndexPath *indexPath = [self indexPathOfSubview:sender];
NSLog(@"button pressed in section %d, at row %d", indexPath.section, indexPath.row);
// ta-da!
}
答案 1 :(得分:0)
将静态更改为动态单元格
static NSString *identifier = @"cell";
到
NSString *identifier = [NSString stringWithFormat:@"cell%d",indexPath.row];
可能是它的工作