0

我有一个应用程序,在该应用程序中,我从两个不同的自定义类在表格视图中加载两种类型的单元格。我的表格视图中有 5 个部分,每个部分都有单行。我在单元格中添加了一个按钮,单击它时会更改按钮的图像.as button.selected=YES;就像那样。但我的问题是当我滚动单元格时重复,即如果我连续选择了一个按钮,那么该行的背景图像只需要更改.但在我的情况下,一些单元格按钮也在改变。这就是我添加东西的方式。

- (UITableVieCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier1 = @"CellIdentifier";
    static NSString *CellIdentifier2 = @"Cell";
    if (indexPath.section == 0 && indexPath.row==0)
    { 
         CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

        if (cell == nil)
        {
            cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier1] autorelease];
            cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"dhjkhs.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];


        }
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.datelabel.text=@"skhfkhdf";
      cell.nameLabel.text=@"dsdbjsbjf";
    cell.msg.text=@"dshjshhshd";
 [cell.lbutton addTarget:self action:@selector(callAction:) forControlEvents:UIControlEventTouchUpInside];     
  cell.myImageView.image=[UIImage imageNamed:@"sdsd.png"];
  cell.llabel.text=@"sdjkjskljdls";   
        cell.llabel.tag=indexPath.section+1;

        return cell;

    }
    else 
    {


        Customcellwithimage *cell = (Customcellwithimage *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

        if (cell == nil)
        {
            cell = [[[Customcellwithimage alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier2] autorelease];
             cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"sdsdsd.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
        }
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.datelabel1.text=@"sdsdsdsd";
        cell.nameLabel1.text=@"sdsdsdsdsd";
        cell.msg1.text=@"sdsasfkdakfhk ";

         [cell.likebutton1 addTarget:self action:@selector(callAction:) forControlEvents:UIControlEventTouchUpInside]; 
        cell.myImageView1.image=[UIImage imageNamed:@"jpg1.png"];
         cell.cellview1.image=[UIImage imageNamed:@"adsdsd.png"];

        cell.llabel1.text=@"sdsdsdsdl";
        cell.llabel1.tag=indexPath.section+2;
       cell.bannerview1.image=[UIImage imageNamed:@"dsdsd.png"];

        return cell;







    }



    return Nil;

}

i have 10 sections each returning 1 row.My problem is i need to change the image of the button when clicked.I was adding that button in the customcellwith image class as

[lbutton1 setImage:[UIImage imageNamed:@"sdsd.png"] forState:UIControlStateNormal];
[lbutton1 setImage:[UIImage imageNamed:@"sdsdsd.png"] forState:UIControlStateSelected];
lbutton1.selected=NO;`

在我的代码中我添加了`

UIButton *button = (UIButton *)sender;
      button.selected=YES;

` 但这里的问题是它也改变了另一个部分的按钮。有人可以帮我吗?

4

2 回答 2

0

也许你的检查条件有问题if (indexPath.section == 0 && indexPath.row==0)NO当它不是第一部分时,此检查将始终。CustomCell因此,除了第一部分的第一行之外,您的单元格不会被您的班级重复使用。

于 2012-11-20T15:51:03.773 回答
0

我不确定我是否完全理解这个问题,但我认为你想将背景创建也移到 if 后面。一旦它被改变,你应该重置它,现在它不会被重置(就像你重置选择样式一样,你应该重置背景)

if (cell == nil)
{
    cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier1] autorelease];
}

cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"dhjkhs.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];

cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.datelabel.text=@"skhfkhdf";
cell.nameLabel.text=@"dsdbjsbjf";
cell.msg.text=@"dshjshhshd";
[cell.lbutton addTarget:self action:@selector(callAction:) forControlEvents:UIControlEventTouchUpInside];     
cell.myImageView.image=[UIImage imageNamed:@"sdsd.png"];
cell.llabel.text=@"sdjkjskljdls";   
cell.llabel.tag=indexPath.section+1;

return cell;
于 2012-11-20T15:28:19.783 回答