繁体   English   中英

在UITableView objective-C中滚动更改数据

[英]Scroll change data in UITableView objective-C

我有一个uitableView,在每一行中我有4个按钮,所有按钮的名称是“W”,

当我运行程序时:我的第一行中的第一个4按钮只有“W”

当我滚动我的桌子时,我会得到更多按钮名称“W”

有谁知道这是什么问题?

提前致谢

我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;

int column = 4;
for (int i=0; i<column; i++) {

    // position in the parent view and set the size of the button
    UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(0.5+83*i,10, 80,115)];
    myButton.tag=(column*indexPath.row)+i;
    [cell.contentView addSubview:myButton];
    [myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    myButton.backgroundColor = [UIColor whiteColor];


    UILabel *btnTitle = [[UILabel alloc] initWithFrame:CGRectMake(25, 10, 30, 10)];
    [btnTitle setText:@"W"];
    [btnTitle setTextColor:[UIColor blackColor]];
    [myButton addSubview:btnTitle];

           }
return cell;
}

在此输入图像描述

阿比泽姆说。 “细胞将有许多按钮......和btnTitles”他是对的。

滚动表视图时,您的代码将调用“内存不足”和“慢速”。

创建一个CustomTableViewCell类。


@interface CustomTableViewCell : UITableViewCell
{
    NSArray *_buttons;
}
@property (nonatomic, strong) IBOutlet UIButton *button1, button2, button3, button4;
@property (nonatomic, strong, readonly) NSArray *buttons;
@end

@implementation CustomTableViewCell
...
@dynamic buttons;
- (NSArray*)buttons
{
   if (_buttons == nil) {
       _buttons = [NSArray arrayWithObjects:self.button1, self.button2, self.button3, self.button4, nil];
   }
   return _buttons; // I fixed it _button --> _buttons
}
@end

  • 创建一个空的xib文件。
  • 拖放UITableViewCell。
  • 并在UITableViewCell上拖放4个UIButton。
  • 选择并将UITableViewCell自定义类更改为CustomTableViewCell。
  • 将button1,button2,button3,button4链接到xibs项目。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = (CustomTableViewCell*)[[[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil] objectAtIndex:0];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
int column = 4;
for (int i=0; i<column; i++) {
    // position in the parent view and set the size of the button
    UIButton *myButton = [cell.buttons objectAtIndex:i];
    //myButton.frame = CGRectMake(0.5+83*i,10, 80,115); You can design on interface builder
    myButton.tag=(column*indexPath.row)+i;
    [cell.contentView addSubview:myButton];
    [myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    myButton.backgroundColor = [UIColor whiteColor];

    //myButton.textLabel.frame = CGRectMake(25, 10, 30, 10); You can design on interface builder
    [myButton.textLabel. setText:@"W"];
    [myButton.textLabel. setTextColor:[UIColor blackColor]];
 }
return cell;
}

  • 抱歉。 不是“ni” - >“nil”
  • return _button - > return _buttons

@interface CustomTableViewCell : UITableViewCell
{
    NSArray *_buttons;
}
@property (nonatomic, strong) IBOutlet UIButton *button1, button2, button3, button4;
@property (nonatomic, strong, readonly) NSArray *buttons; // <----- Check it.
@end

我不能评论答案,因此我改变了这个答案 boiljong的解决方案是正确的。 作为booiljoung的帖子,您可以在故事板中轻松使用它。 首先,您应该在故事板中自定义UITableViewCell。 UITableView的第一行是UITableViewCell(如果在将故事板添加到故事板时没有删除它)。 然后添加4个按钮并将其类设置为CustomUITableViewCell。 将按钮链接到CustomUITableViewCell。 最重要的是不要忘记将标识符更改为您在代码tableview dequeueReusableCellWithIdentifier中使用的标识符 请参阅我上传的屏幕截图。 而你的代码中的错误是由打字错误造成妮应该是NI L和_button应该_buttons 故事板中的自定义UITableViewCell

旧答案只有当单元格为零时才需要创建按钮,如果单元格不是nil,则表示tableview正在重复使用按钮创建的“旧”单元格。 调用dequeueReusableCellWithIdentifier时,如果可能,它始终重用单元格。 例如,使用那些不在当前屏幕中的单元格。 他们已经有4个名为“W”的按钮。 因此,每次滚动时,每次进入单元格时都会有4个按钮。 如果(cell == nil){...},您可以将按钮创建代码移动到代码括号中

但滚动到新行时很难操纵按钮的标题。 最好用自己的方法自定义UITableViewCell。 您可以在http://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW18中找到该参考资料。

因为您已从下面的粘贴代码中写出了Button

if (cell == nil) { cell = [[UITableViewCell alloc] initWithFrame:CGRectZero]; }

您可以复制该行代码,然后将其粘贴到下面的代码行之后

cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];

还有一个人认为你是单独创建文本标签,即使你可以给按钮标题

[myButton setTitle:@"Somethig" forState:UIControlStateNormal];

通过这个你也可以节省内存

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM