gpt4 book ai didi

ios - 为 UITableView 的不同部分设置按钮标签

转载 作者:行者123 更新时间:2023-12-01 17:13:18 25 4
gpt4 key购买 nike

我列出了来自 Web 服务的联系人并将其显示在联系人“分段”tableView 中,如屏幕截图所示。

enter image description here

问题是我为 A 部分和 S 部分的第一行的复选框获得了相同的标记值。我已经对一个数组进行了排序并显示在索引表 View 中。无论显示的部分数量如何,如何根据 indexPath.row 获取不同的标记值。这是我尝试过的

    In cellForRowAtIndexPath: 
UIButton *checkBox;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
checkBox = [[UIButton alloc]initWithFrame:CGRectMake(7, 8, 30, 30)];
}
else
{
checkBox = [[UIButton alloc]initWithFrame:CGRectMake(15, 13, 30, 30)];
}

//int cnt = 0;
// for (int i = indexPath.section - 1; i > 0 ; i--)
// {
// cnt += [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:i]] count]; //arrayOfCharachters has char 'A' to 'Z'
// }

//checkBox.tag = cnt + indexPath.row;

[checkBox setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
[checkBox addTarget:self action:@selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
[checkBox setTag:indexPath.row];
[cell.contentView addSubview:checkBox];
return cell;
}

-(void)checkBoxClicked:(id)sender
{

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableViewContact];
NSIndexPath *indexPath = [self.tableViewContact indexPathForRowAtPoint:buttonPosition];
UIButton *tappedButton = (UIButton*)sender;
NSLog(@"Tag number = %d", [sender tag]);

if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"checkBox.png"]])
{
[sender setImage:[UIImage imageNamed: @"checkBoxMarked.png"] forState:UIControlStateNormal];
if(indexPath != Nil)
{
NSString *finalIntId = [mutableArrayOfIds objectAtIndex:indexPath.row]; // store check box ids in mutableArrayOfIds
NSLog(@"Tagged checked button id = %@", finalIntId);
[arrayOfIds addObject:finalIntId];
}
//NSString *finalIntId = [mutableArrayOfIds objectAtIndex:tappedButton.tag];
//NSString *finalIntId = [mutableArrayOfIds objectAtIndex:indexPath.row];
}
else
{
[sender setImage:[UIImage imageNamed:@"checkBox.png"]forState:UIControlStateNormal];
NSLog(@"UnChecked");
//[arrayOfIds removeObjectAtIndex:tappedButton.tag];

}
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

if ([arrayOfCharacters count] == 0)
{
return @"";
}

return [NSString stringWithFormat:@"%@", [arrayOfCharacters objectAtIndex:section]];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSArray *toBeReturned = [NSArray arrayWithArray:
[@"A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|#"
componentsSeparatedByString:@"|"]];

return toBeReturned;

}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{


NSInteger count = 0;

for (NSString *character in arrayOfCharacters) {

if ([character isEqualToString:title]) {
return count;
}

count ++;
}

return 0;


}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

return [arrayOfCharacters count];
//return 1;

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{


//return [mutableArray count];
return [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:section]] count];

}

最佳答案

您正在为具有相同行的所有部分设置相同的标记值。
indexPath 有两个属性,{section,row}。

假设 A 部分有两行,

for row1 -> indexPath.section=0, indexPath.row=0;
for row2-> indexPath.section=0, indexPath.row=1;

假设 S 部分有两行,
for row1 -> indexPath.section=1, indexPath.row=0;
for row2-> indexPath.section=1, indexPath.row=1;

因此,对于 A 部分的 row1 和 S 部分的 row1,您正在设置相同的标记值,即 0。这是您的问题。

尝试设置标签值,如下所示。
button.tag = indexPath.section*1000 +indexPath.row;

检索部分和行时,
NSInteger section = (button.tag)/1000;
NSInteger row = (button.tag)%1000;

关于ios - 为 UITableView 的不同部分设置按钮标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21227618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com