我在这个tableView上有一个tableView和一个tableViewCell。我为tableViewCell定义了另一个名为CustomCell的类,以便我编写必要的自定义项并创建一个按钮(在此单元格上)。当单击tableViewCell上的按钮时,我想知道哪个tableViewCell包含该按钮,以便我只能对该单元格进行必要的更改(其中包含单击的按钮)
我如何理解哪个tableViewCell包含单击的按钮?
答案 0 :(得分:0)
如果使用自定义按钮替换附件详细信息按钮,则可以调用accessoryButtonTappedForRowWithIndexPath:方法。
示例 - 将其放在cellForRowAtIndexPath中(设置单元格时):
UIButton *myAccessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 125, 24)];
[myAccessoryButton setImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];
[cell setAccessoryView:myAccessoryButton];
[myAccessoryButton release];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
然后作为一个单独的方法:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath: (NSIndexPath *)indexPath
{
//do something
}
答案 1 :(得分:0)
实现此目的的一个策略是将带有行号的标记分配给按下的按钮:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexpath {
YourCustomCell *cell = // Code
cell.button.tag = indexPath.row;
// Other cell preparation code
return cell;
}
然后,在按钮的操作中,您可以看到它的标记是什么来确定对应于的模型对象:
- (void)didSelectButton:(id)sender {
UIButton *button = sender;
NSInteger tag = button.tag;
YourModelObject *item = self.items[tag];
// Continue with the business logic for what should
// happen when that button is pressed.
}
答案 2 :(得分:0)
这个怎么样......
- (UITableViewCell *)cellWithSubview:(UIView *)subview {
while (subview && ![subview isKindOfClass:[UITableViewCell self]]) subview = subview.superview;
return (UITableViewCell *)subview;
}
- (IBAction)buttonClicked:(id)sender {
UITableViewCell *theCell = [self cellWithSubview:sender];
}
答案 3 :(得分:0)
您需要以这种方式调用此cellForRowAtIndexPath
方法。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10 , 10, 200, 20);
[button addTarget:self action:@selector(passRowValueMethod:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:indexPath.row];
[button setTitle:[NSString stringWithFormat:@"%d", indexPath.row] forState:UIControlStateNormal];
[cell addSubview:button];
return cell;
}
然后在此方法中定义按钮目标方法并获取标记值。
-(void)passRowValueMethod:(UIButton*)sender
{
UIButton *buttonGet = sender;
NSLog(@"%d", buttonGet.tag);
}
希望这会对你有所帮助。
答案 4 :(得分:0)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 10.0, 24, 24);
button.frame = frame;
[button setTag:((indexPath.section & 0xFFFF) << 16) |
(indexPath.row & 0xFFFF)];
[button setImage:[UIImage imageNamed:@"link-buttoni4.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"link-button-onclicki4.png"] forState:UIControlStateHighlighted];
[button setSelected:NO];
// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
[button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
}
- (void)checkButtonTapped:(UIButton *)sender {
NSUInteger section = ((sender.tag >> 16) & 0xFFFF);
NSUInteger row = (sender.tag & 0xFFFF);
NSLog(@"Button in section %i on row %i was pressed.", section, row);
}
答案 5 :(得分:0)
试试这个,
你可以知道在这种方法中单击一个单元格的节和行号:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
}
1&gt;部分可以通过
获得
int sectionno = indexPath.section
2>细胞指数可以通过
获得
int rowno = indexPath.row
然后使用你就可以得到像这样的表格单元格,
UITableViewCell *cell=[product_table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowno inSection:sectionno]];