如何在iphone sdk中设置类似按钮标签和单元格标签

时间:2011-05-26 12:41:26

标签: iphone

我有一个包含标签和按钮的表。在该按钮上我生成了下拉列表。我想设置类似的单元格标签和按钮标签。 b'cos根据我必须保持我的功能。供你参考后面的图片给你一个想法

As per image, Suppose consider i am having 4 records means the "indexpath.row" for cell is 0-11 now i want to set my button tag and cell tag according following series 0 1 2 3 3 4 5 5 6 7 7 8 9 9 10 11 11  and so on b'cos data records are dynamic.

这是我在CellForRowAtIndexPath中的代码:-NSString * s;     VAR = 0;

if(indexPath.row<=2)
{
  btn_click.tag=indexPath.row;

    s =[dict valueForKey:[NSString stringWithFormat:@"%d",indexPath.row]];
}
else
{
    if (indexPath.row%3==0)
    {
        Var=(NSInteger)(indexPath.row/3);

    }
    else
    {
        Var=Var+1;
    }

    if(indexPath.row%3==0 && Var==1)
    {       

        btn_click.tag=indexPath.row;
         s =[dict valueForKey:[NSString stringWithFormat:@"%d",indexPath.row]];

    }

    else 
    {
        if (Var>1)
        {
            Var=1;
        }
        btn_click.tag=indexPath.row-Var;
        s =[dict valueForKey:[NSString stringWithFormat:@"%d",indexPath.row-Var]];
    }

}
NSLog(@"CELLTAGTag%d",btn_click.tag);  

}

谢谢&amp;问候, 普里。

2 个答案:

答案 0 :(得分:2)

如果要为单元格行号上的cellView和buttonView depanding提供相同的标签号,可以执行以下操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *CellIdentifier = @"CustomTableCell";
    static NSString *CellNib = @"UserCustomTableCell";

    UserCustomTableCell *cell = (UserCustomTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
        cell = (UserCustomTableCell *)[nib objectAtIndex:0];
    }

    cell.myButton.tag = [indexPath.row];
    cell.tag = [indexPath.row];

    return cell;
}

答案 1 :(得分:0)

将Button标记为indexPath.row。因此,您可以使用行值获取单元格,该值也是tag

UIButton
yourButton.tag = indexPath.row