【问题标题】:Table cell reusing button in different section iOS使用不同部分 iOS 中的按钮的表格单元格
【发布时间】:2013-06-12 09:17:12
【问题描述】:

我创建了UITableView,其中包含 4 个具有动态行数的部分,我自定义了单元格并将按钮添加到非常单元格,按钮在不同的部分中重复使用。

我的代码:

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

      }


     self.buttonMe = [UIButton buttonWithType:UIButtonTypeCustom];
    [ self.buttonMe setFrame:CGRectMake(260.0, 7.0, 30.0, 30.0)];
    self.buttonMe.tag=i;

    if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])

    [ self.buttonMe setImage:[UIImage imageNamed:@"check.gif"] forState:UIControlStateNormal];

    else

    [ self.buttonMe setImage:[UIImage imageNamed:@"uncheck.jpg"] forState:UIControlStateNormal];

    [ self.buttonMe addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview: self.buttonMe];

    cell.textLabel.text=@"itsFine";
//"i", i created for assigning the unique tag for created button every time
    i++;

    return cell;

}

请给出正确的解决方案并说明其发生的原因 谢谢

【问题讨论】:

  • 您还没有自定义单元格。 ButtonMe 应该是自定义单元格的一部分,cell.buttonMe 是应该改变的,你正在做的是重用你的主类的实例变量。
  • 我添加了自定义按钮作为 cell.contentView 的子视图,
  • 请清除你的行你正在做的是重用你的主类的实例变量
  • self.buttonMe 每次都被添加到单元格中。您可以做的是创建一个自定义单元格并拥有一个变量 buttonMe 并通过 cell.buttonMe 访问它。这样您就不会遇到细胞重复使用的问题。
  • 请 praveen 帮助我,如何实现这一点,请请

标签: iphone uitableview uibutton cell reusability


【解决方案1】:

警告:它未经测试,未编译的代码只是为了给你一个想法。

好的,我将尝试概述创建自定义 uitableview 单元格的方法

@interface MyCustomCell  : UITableViewCell

@property (nonatomic, strong) UIButton * buttonMe

@end

实现将具有以下内容

@implementation MyCustomCell 
@synthesize buttonMe;

- (UIButton *) buttonMe {
     if(!buttonMe){
      buttonMe = [UIButton buttonWithType:UIButtonTypeCustom];
      [buttonMe setFrame:CGRectMake(260.0, 7.0, 30.0, 30.0)];
     }
return buttonMe;
}

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if (( self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] )) {
     [self addSubView:self.buttonMe];
      }
 }

@end

接下来你可以修改你的代码如下:

    static NSString *CellIdentifier = @"Cell";
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil){
            cell = [[UITableViewCell alloc] initWithReuseIdentifier:CellIdentifier];
          }
        [cell.buttonMe setFrame:CGRectMake(260.0, 7.0, 30.0, 30.0)];
        cell.buttonMe.tag=i;

        if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"]){
        [cell.buttonMe setImage:[UIImage imageNamed:@"check.gif"] forState:UIControlStateNormal];
}
        else{
        [cell.buttonMe setImage:[UIImage imageNamed:@"uncheck.jpg"] forState:UIControlStateNormal];
}
        cell.textLabel.text=@"itsFine";
        i++;

        return cell;

最后在您的自定义类中添加按钮点击处理程序。

【讨论】:

    【解决方案2】:

    从您的问题中不清楚您要问什么。如果我没记错的话,你有 4 个部分,每个部分都有一些行(单元格),每行都有选中和取消选中按钮。您正在标记按钮,以便您可以获取该按钮的索引来执行某些操作。

    现在,记住cellForRowAtIndexPath 会在您每次向上或向下滚动表格视图时被调用。因此,每次滚动表格视图时,“i”都会增加。

    因此,如果您有 10 行并且您认为您已将每行的 i = 0 设置为 9。但是当你每次滚动时,“i”都会增加。因此,您可能无法在代码中进一步使用标记值。

    更好的是使用indexPath.row 的tableview。这对于每一行都是唯一的。

    现在修改您的代码,

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {
         static NSString *CellIdentifier = @"Cell";
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
         UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
         if (cell == nil)
         {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
            /*Reusing you button*/
            [myButton setFrame:CGRectMake(260.0, 7.0, 30.0, 30.0)];
            [myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:myButton];
            cell.textLabel.text = @"itsFine";
          }
          myButton.tag = indexPath.row; // Should not use i at here
    
        if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
           [myButton setImage:[UIImage imageNamed:@"check.gif"] forState:UIControlStateNormal];
        else
           [myButton setImage:[UIImage imageNamed:@"uncheck.jpg"] forState:UIControlStateNormal];
    
        return cell;
    }
    

    【讨论】:

    • 我采取了你的观点,但是,问题在于,加载视图时它没有显示按钮,但是当我滚动它时它就来了,其次,当我选择第 0 部分的第一行而不是所有部分的第一行行被选中,请解决它
    • 是的,因为您使用的是同一个按钮对象。因此,当您选择一个按钮时,所有按钮都会被选中。我正在编辑 mu 以前的答案,看看它是否有效。
    • myButton 在 cell 为 nil 时被添加,当 cell 开始被重用时会发生什么。这里有很多问题。
    猜你喜欢
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多