3608
5625
我列出了来自Web Service的联系人,并将其显示在联系人“分段”的tableView中,如屏幕截图所示。
问题是我为A部分和S部分的第一行的复选框获得了相同的标记值。我对一个数组进行了排序并显示在索引表视图中。如何根据indexPath.row获得不同的标记值,而与显示的段数无关。这是我尝试过的
在cellForRowAtIndexPath中:
UIButton * checkBox;
if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone)
{
checkBox = [[UIButton alloc] initWithFrame:CGRectMake(7,8,30,30)];
}
其他
{
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的字符从“ A”到“ Z”
//}
//checkBox.tag = cnt + indexPath.row;
[checkBox setImage:[UIImage imageNamed:@“ checkBox.png”] forState:UIControlStateNormal];
[checkBox addTarget:self操作:@selector(checkBoxClicked :) forControlEvents:UIControlEventTouchUpInside];
[checkBox setTag:indexPath.row];
[cell.contentView addSubview:checkBox];
返回单元
}
-(void)checkBoxClicked:(id)发件人
{
CGPoint buttonPosition = [发送方convertPoint:CGPointZero toView:self.tableViewContact];
NSIndexPath * indexPath = [self.tableViewContact indexPathForRowAtPoint:buttonPosition];
UIButton * tappedButton =(UIButton *)sender;
NSLog(@“标签号=%d”,[发送方标签]);
if([[tappedButton.currentImage isEqual:[UIImage imageNamed:@“ checkBox.png”]])
{
[发件人setImage:[UIImage imageNamed:@“ checkBoxMarked.png”] forState:UIControlStateNormal];
if(indexPath!=无)
{
NSString * finalIntId = [mutableArrayOfIds objectAtIndex:indexPath.row]; //将复选框ID存储在mutableArrayOfIds中
NSLog(@“已标记的已选中按钮ID =%@”,finalIntId);
[arrayOfIds addObject:finalIntId];
}
// NSString * finalIntId = [mutableArrayOfIds objectAtIndex:tappedButton.tag];
// NSString * finalIntId = [mutableArrayOfIds objectAtIndex:indexPath.row];
}
其他
{
[发件人setImage:[UIImage imageNamed:@“ checkBox.png”] forState:UIControlStateNormal];
NSLog(@“ UnChecked”);
// [arrayOfIds removeObjectAtIndex:tappedButton.tag];
}
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
如果([arrayOfCharacters count] == 0)
{
返回@“”;
}
返回[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:@“ |”]];
返回到返回
}
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
NSInteger计数= 0;
for(NSString * arrayOfCharacters中的字符){
如果([character isEqualToString:title]){
返回计数;
}
数++;
}
返回0;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
返回[arrayOfCharacters count];
//返回1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)部分
{
//返回[mutableArray count];
返回[[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:section]]计数];
} 
您要为具有相同行的所有部分设置相同的标签值。
indexPath具有两个属性{section,row}。
假设A部分有两行,
对于row1-> indexPath.section = 0,indexPath.row = 0;
对于row2-> indexPath.section = 0,indexPath.row = 1;
假设S部分有两行,
对于row1-> indexPath.section = 1,indexPath.row = 0;
对于row2-> indexPath.section = 1,对于indexPath.row = 1;
因此,对于A部分的第1行和S部分的第1行,您要设置相同的标记值0,这就是问题所在。
尝试如下设置标签值。
button.tag = indexPath.section * 1000 + indexPath.row;
检索节和行时,
NSInteger部分=(button.tag)/ 1000;
NSInteger行=(button.tag)%1000;
|
试试这个...
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
静态NSString * CellIdentifier = @“ Cell”;
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
单元格= [[UITableViewCell分配] initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
}
UIButton * checkBox = [[UIButton alloc] initWithFrame:CGRectMake(280,10,50,44)];
checkBox.backgroundColor = [UIColor orangeColor];
[checkBox addTarget:self操作:@selector(checkBoxClicked:event:)forControlEvents:UIControlEventTouchUpInside];
[checkBox setTag:indexPath.row];
[cell.contentView addSubview:checkBox];
返回单元
}
-(void)checkBoxClicked:(id)发送者事件:(id)事件
{
NSSet * touches = [event allTouches];
UITouch * touch = [触摸anyObject];
CGPoint currentTouchPosition = [触摸locationInView:self.tv]; //这里电视是TableView对象
NSIndexPath * indexPath = [self.tv indexPathForRowAtPoint:currentTouchPosition];
NSLog(@“ indePath.section%d的值,indexPath.row%d”,indexPath.section,indexPath.row);
}
|
之所以发生这种情况,是因为您正在将标签分配给各个部分的按钮INDEPENDENT。
A和S部分的第一行都具有row =0。因此分配给它们各自按钮的标签为0。您应该为其分配名称,以保持对部分的引用。
我建议使用包含Section,Row的逗号分隔形式分配可访问性提示。
而用你的方法
-(void)checkBoxClicked:(id)发件人
{
//从逗号分隔的形式中选择字符串。第一个是您的部分,第二个是行。
}
第二个选择是执行您所做的一切,并像这样实现Button方法。
CGPoint buttonPosition = [发送方convertPoint:CGPointZero toView:self.tableView];
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
如果(indexPath!= nil)
{
... indexpath.section是您的部分,index path.row是您的行。
}
还有第三种选择。
在cellforRowAtIndexpath中为您的Button分配一个标题
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
[btn setTitle:<#您的行#> forState:UIControlStateDisabled];
[btn setTag <#您的部分#>];
因此,在接收您的Button方法后,您可以同时拥有Section(标签)和Row(禁用标题)。
-(void)checkBoxClicked:(id)sender {[button titleForState:UIControlStateDisabled]; //您的行
button.tag //您的部分}
|
试试这个!
您可能会知道每个部分的正确计数,然后添加各个行的计数。
[[[fieldTitlesList objectAtIndex:indexPath.section-1] count] + indexPath.row
其中fieldTitlesList是您的部分的数组。
|
我添加了以下代码来解决我的问题
NSInteger rowNumber = 0;
for(NSInteger i = 0; i