如何在uitableview ios中获取viewwithtag的UIButton的索引 [英] How to get index of UIButton that viewwithtag in uitableview ios
本文介绍了如何在uitableview ios中获取viewwithtag的UIButton的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题:我想在 tableview 中获取 UIButton 的索引.我创建的 uitableview 每行有 2 个 uibutton,但是当我点击 uibutton 时,它的索引不正确.代码创建 UITableview :
I have a problem: I want to get index of UIButton in tableview. I created uitableview has 2 uibutton in each row, but when i click on uibutton, it get index incorrect. Code create UITableview :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"AllItemsArrayarray: %@", temp);
return [temp count];
}
cellForRowAtIndexPath
函数
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[market setTag:4000];
[market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[cell.contentView addSubview:market];
}
UIButton *marketButton = (UIButton*)[cell.contentView viewWithTag:4000];
if([sellingArray count]>0)
{
NSLog(@"sellingArray %@",sellingArray);
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]) // nothing
{
[marketButton setSelected:NO];
[marketButton setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal];
marketButton.enabled = YES;
}
else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"]) // marketplace
{
[marketButton setSelected:YES];
[marketButton setImage:[UIImage imageNamed:@"MarketplaceSelect.png"] forState:UIControlStateNormal];
marketButton.enabled = YES;
}
}
return cell;
}
和marketPressedAction
函数
- (void)marketPressedAction:(id)sender
{
buttonPressed = (UIButton *)sender;
buttontag = buttonPressed.tag ;
NSLog(@"Market button click at row %d",buttontag);
}
当表格有 5 行时,我点击按钮,它显示错误:
When table has 5 row, I click on button, it shows error:
-[__NSArrayM objectAtIndex:]: index 4000 beyond bounds [0 .. 4]'
推荐答案
you set tag using indexpath.row
you set tag using indexpath.row
UIButton *market ;
if (nil == cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
market = [UIButton buttonWithType:UIButtonTypeCustom];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[market setTag:indexPath.row];
[market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
if([sellingArray count]>0)
{
NSLog(@"sellingArray %@",sellingArray);
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]) // nothing
{
[market setSelected:NO];
[market setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal];
market.enabled = YES;
}
else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"]) // marketplace
{
[market setSelected:YES];
[market setImage:[UIImage imageNamed:@"MarketplaceSelect.png"] forState:UIControlStateNormal];
market.enabled = YES;
}
[cell.contentView addSubview:market];
这篇关于如何在uitableview ios中获取viewwithtag的UIButton的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文