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

3
我从Web服务中获取联系人列表,并将其显示在分组的tableView中,如屏幕截图所示。
问题是我得到了第一行的复选框的相同标记值,无论是A部分还是S部分。我已经对一个数组进行了排序,并在索引表视图中显示。如何根据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];

}

更好的方法是对TableView的Cell进行子类化,这样您就可以获取整个单元格的所有信息,包括节和行。 - Shankar BS
5个回答

21

当你设置相同的标签值时,应用于所有具有相同行的部分。
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的行1和部分S的行1,您设置了相同的标签值为0。这就是您的问题所在。

尝试像下面这样设置标签值。

button.tag = indexPath.section*1000 +indexPath.row;

在检索章节和行时,

NSInteger section = (button.tag)/1000;
NSInteger row = (button.tag)%1000;

关于最后两行,我该如何使用“section”和“row”将标签值存储在数组中? - Deepak Thakur
为什么需要将tagValue存储在数组中?因为您不需要存储它,因为它可以通过sender.tag知道。 - santhu
好的,明白了。你不能将NSIntegers存储在数组中,你应该将其转换为一个Objective-C对象NSNumber。使用[NSNumber numberWithInt:button.tag];希望这可以帮到你。如果想要从这个数字中获取整数,请使用number.integerValue。 - santhu
那么我该如何合并部分和行,例如[NSNumber numberWithInt:(section + row)button.tag],而不是[NSNumber numberWithInt:button.tag]呢? - Deepak Thakur
当您设置按钮的标记时,按照答案中所示进行设置。 button.tag = indexPath.section*1000 +indexPath.row; 现在您的按钮标记中存储了部分和行。 因此,将其添加到数组[NSNumber numberWithInt:button.tag];即可正常工作。 - santhu
显示剩余3条评论

2
Try this...



- (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];

    }
    UIButton *checkBox =[[UIButton alloc] initWithFrame:CGRectMake(280, 10, 50, 44)];
    checkBox.backgroundColor =[UIColor orangeColor];
    [checkBox addTarget:self action:@selector(checkBoxClicked:event:)forControlEvents:UIControlEventTouchUpInside];
    [checkBox setTag:indexPath.row];
    [cell.contentView addSubview:checkBox];

    return cell;
}
- (void)checkBoxClicked:(id)sender event:(id)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tv]; //here tv is TableView Object
    NSIndexPath *indexPath = [self.tv indexPathForRowAtPoint: currentTouchPosition];

    NSLog(@"value of indePath.section %d ,indexPath.row %d",indexPath.section,indexPath.row);

}

如何根据indexPath.section和indexPath.row将标签值存储在数组中。例如:[array addObject:[arrayOfIds objectAtIndex:(indexPath.section + indexPath.row)]]; - Deepak Thakur

1
这是由于您独立于部分为按钮分配标记。 A和S部分的第一行都有row = 0。因此,分配给它们各自按钮的标记为0。您应该在保持对部分的引用的情况下将它们分配。 我建议使用逗号分隔的形式分配可访问性提示,其中包含Section,Row。
在您的方法中 -(void)checkBoxClicked:(id)sender { //从逗号分隔的形式中获取字符串。第一个是您的部分,第二个是行。 }
第二个选择是按照您正在执行的操作实现按钮方法。
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    if (indexPath != nil)
    {
     ... indexpath.section is your section , index path.row is your row.
    }

还有第三个选项。

cellforRowAtIndexpath中为您的按钮分配一个标题 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath [btn setTitle:<#Your Row#> forState:UIControlStateDisabled]; [btn setTag<#Your Section#>];

因此,在接收到您的按钮方法时,您可以同时拥有部分(标签)和行(禁用状态的标题)。

-(void)checkBoxClicked:(id)sender { [button titleForState:UIControlStateDisabled]; // your Row

button.tag //你的部分 }


0

试试这个!

每个部分你可能知道部分的数量,然后再加上每行的数量。

 [[fieldTitlesList objectAtIndex:indexPath.section - 1] count] + indexPath.row

其中 fieldTitlesList 是您的章节数组。


0
我添加了以下代码,解决了我的问题。
NSInteger rowNumber = 0;
    for(NSInteger i = 0; i < indexPath.section ; i++)
    {
        rowNumber += [self tableView:self.tableViewContact numberOfRowsInSection:i];
    }

    rowNumber += indexPath.row;

[checkBox setTag:rowNumber]; //checkBox is UIButton in cellForRowAtIndexPath

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接