UITwitch和UIButton在UITableViewCell中,如何识别它们 [英] UISwitch and UIButton in UITableViewCell, how to ident them
问题描述
我的UITableViewCell有点问题。
每个自定义单元都有一个UISwitch和一个UIButton。当用户更改UISwitch的值时,我想将button.hidden属性设置为YES。
I've got a little problem in my UITableViewCell. Each Custom Cell have an UISwitch and an UIButton. And when the user change the value of the UISwitch i would like to set the button.hidden property to YES.
我可以找到用这种方法点击的UISwitch: / p>
I can find which UISwitch was clicked with this method:
- (IBAction)closeHour:(id)sender {
UISwitch *senderSwitch = (UISwitch *)sender;
UITableViewCell *switchCell = (UITableViewCell *)[senderSwitch superview];
NSUInteger buttonRow = [[resas indexPathForCell:switchCell] row];
NSLog("%d", buttonRow);
}
这项工作完美,但我不知道如何获得UIButton相同的索引(indexPath.row)来设置他的隐藏属性。我想在每个UIButton中设置一个标签,但我对这些标签不是很友好。
This work perfectly, but i don't know how to get the UIButton at the same index (indexPath.row) to set his hidden property. I thought about setting a tag in each UIButton but i'm not very friendly with those.
如果有人可以告诉我我是不是在这里做一些垃圾可能非常好:
There is how my Cell was created, if someone can tell me if i am doing some crap here it could be very nice:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier1 = @"Cell1";
static NSString *cellIdentifier2 = @"Cell2";
if ([typeOfData objectAtIndex:indexPath.row] == @"hour") {
TimeCell *cell = (TimeCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
if (cell == nil) {
cell = [[[TimeCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier1] autorelease];
UISwitch *isOpen = [[UISwitch alloc] initWithFrame:CGRectMake(300, 7, 0, 0)];
if ([self openOrCloseHour:[[[finalData objectAtIndex:indexPath.row] substringToIndex:2] intValue]])
isOpen.on = YES;
else {
isOpen.on = NO;
[isOpen addTarget:self action:@selector(closeHour:) forControlEvents:UIControlEventValueChanged];
[cell addSubview:isOpen];
[isOpen release];
UIButton *add = [UIButton buttonWithType:UIButtonTypeContactAdd];
add.frame = CGRectMake(400, 3, 170, 40);
[add addTarget:self action:@selector(addResa:) forControlEvents:UIControlEventTouchDown];
[cell addSubview:add];
}
}
[cell.time setText:[finalData objectAtIndex:indexPath.row]];
return cell;
}
else
{
ResaCell *cell = (ResaCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
if (cell == nil) {
cell = [[[ResaCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier2] autorelease];
[cell.isConfirm setImage:[UIImage imageNamed:@"confirm.png"]];
[cell.nom setText:[finalData objectAtIndex:indexPath.row]];
[cell.prenom setText:[finalData objectAtIndex:indexPath.row]];
[cell.arrive setText:[finalData objectAtIndex:indexPath.row]];
}
return cell;
}
return nil;
}
有关信息,我有两种类型的单元格。问题在于TimeCell。
For information I have got two type of cell. The problem is on TimeCell.
有人有解决方案吗?
推荐答案
在cellForRow中:
In the cellForRow :
isOpen.tag = 2000+indexPath.row;
add.tag = 4000+indexPath.row;
要在closeHour IBACtion中获取UIButton:
To get the UIButton in the closeHour IBACtion make :
int tagButton = senderSwitch.tag-2000+4000;
button = (UIButton*)[self.view viewWithTag:tagButton];
这篇关于UITwitch和UIButton在UITableViewCell中,如何识别它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!