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

时间:2014-01-20 06:34:23

标签: ios uitableview

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

enter image description here

问题是,对于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];

}

5 个答案:

答案 0 :(得分:18)

您为具有相同行的所有部分设置相同的标记值 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;

答案 1 :(得分: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);

}

答案 2 :(得分:1)

这种情况正在发生,因为您要为部分的INDEPENDENT按钮分配标签。 第一排A和A部分都是S有行= 0.因此分配给各自按钮的标签为0.您应该为它们分配保持对您的部分的引用。 我建议用包含Section,Row的逗号分隔形式指定辅助功能提示。

在你的方法中 -(void)checkBoxClicked:(id)sender { //pick string from comma separated form. 1st is your section, 2nd is row. }

第二个选项是做你做过的事情并像这样实现你的Button方法。

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

因此,在按钮方法中接收时,您可以同时拥有Section(标记)和Row(禁用状态标题)。

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

button.tag //您的部分}

答案 3 :(得分:0)

试试这个!

每个部分你可以知道部分的权利,然后添加个别行的计数。

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

其中fieldTitlesList是您的部分的数组。

答案 4 :(得分: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