Ios 在UITable滚动时自动将UI元素添加到UIStackView

Ios 在UITable滚动时自动将UI元素添加到UIStackView,ios,objective-c,uitableview,uistackview,Ios,Objective C,Uitableview,Uistackview,在我的应用程序中有一个UITable。在表格单元格中,我添加了UIStackView以在加载表格单元格时填充 它工作得很好,直到向下滚动表格,当我上下滚动时,堆栈视图添加了更多元素。(元素意味着UIButtons,我以后会用UIlabel替换它们) 我没有办法解决这个问题。谢谢 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { U

在我的应用程序中有一个UITable。在表格单元格中,我添加了UIStackView以在加载表格单元格时填充

它工作得很好,直到向下滚动表格,当我上下滚动时,堆栈视图添加了更多元素。(元素意味着UIButtons,我以后会用UIlabel替换它们)

我没有办法解决这个问题。谢谢

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    UIStackView *stkItems=(UIStackView *)[cell viewWithTag:8];

    for(int i=0;i<5;i++)    {
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:@"test btn" forState:UIControlStateNormal];
        [stkItems addArrangedSubview:btn];
    }
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UIStackView*stkItems=(UIStackView*)[带标记的单元格视图:8];

对于(int i=0;i从该代码可以看出,每次应用程序需要绘制单元格时,它都会向UIStackView添加按钮,因此当单元格被重用时(由dequeueReusableCellWithIdentifier重用)它仍然包含按钮,但您的代码每次都会添加更多。也许您应该检查UIStackView是否有足够的按钮,或者清除所有按钮并添加所需的内容


希望这对你有所帮助。从这段代码来看,似乎每次你的应用程序需要绘制一个单元格时,它都会在UIStackView中添加按钮,所以当一个单元格被重用时(通过dequeueReusableCellWithIdentifier)它仍然包含按钮,但您的代码每次都会添加更多。也许您应该检查UIStackView是否有足够的按钮,或者清除所有按钮并添加所需的内容


希望这有助于表格视图在单元格离开屏幕时重用单元格。这就是为什么您要发送一条名为
dequeuereusabledCellWithIdentifier:
的消息。您从不从堆栈视图中取出按钮。每次重用单元格时,您都会在已在堆栈视图中的按钮上再添加五个按钮确认视图。

表格视图在单元格离开屏幕时重复使用它们。这就是为什么您要发送一条名为
退出队列可重复使用的CellWithIdentifier:
的消息。您从不从堆栈视图中取出按钮。每次重复使用单元格时,您会在堆栈视图中已有的按钮上再添加五个按钮。我想我已经解决了。但我不知道它是否符合标准

这是最新的。 我已初始化,并在启动时将其声明为0

@implementation HistoryViewController
int data=0; 
并像这样更改cellForRowAtIndexPath,这样它就不会再次更新相同的stackview

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",indexPath.row);
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];


    if(data <=indexPath.row)
    {
        data=(int)indexPath.row;
        UIStackView *stkItems=(UIStackView *)[cell viewWithTag:8];
        [stkItems.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];
        for(int i=0;i<5;i++)    {
            UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];
            [btn setTitle:@"test btn" forState:UIControlStateNormal];
            [stkItems addArrangedSubview:btn];
        }
    }



    return cell;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSLog(@“%ld”,indexath.row);
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

如果(数据我想我解决了它。但我不知道它是否符合标准

这是最新的。 我已初始化,并在启动时将其声明为0

@implementation HistoryViewController
int data=0; 
并像这样更改cellForRowAtIndexPath,这样它就不会再次更新相同的stackview

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",indexPath.row);
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];


    if(data <=indexPath.row)
    {
        data=(int)indexPath.row;
        UIStackView *stkItems=(UIStackView *)[cell viewWithTag:8];
        [stkItems.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];
        for(int i=0;i<5;i++)    {
            UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];
            [btn setTitle:@"test btn" forState:UIControlStateNormal];
            [stkItems addArrangedSubview:btn];
        }
    }



    return cell;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSLog(@“%ld”,indexath.row);
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

如果(data由于单元格正在重用UIStackView,您必须在添加新的子视图之前删除任何子视图。为此,只需调用以下代码,然后再循环并将排列的子视图添加到UIStackView中:

Swift 3

for view in stackView.arrangedSubviews {
    view.removeFromSuperview()
}
目标-C

for (UIView *view in stackView.arrangedSubviews) {
    [view removeFromSuperview];
}
*注意:根据苹果公司的评论,如果你调用[stackView removeArrangedSubview:view],它实际上不会将其完全从接收器中移除。请参阅下面的引用:

-(无效)删除已安排的子视图:(UIView*)视图 “从排列的子视图列表中删除子视图,而不将其作为 接收器的子视图。 要将视图作为子视图删除,请按常规发送-removeFromSuperview; 相关UIStackView会将其从arrangedSubviews列表中删除 自动地。”


由于单元格正在重用UIStackView,因此在添加新的子视图之前,您必须删除任何子视图。为此,在循环并将排列的子视图添加到UIStackView之前,只需调用以下代码即可:

Swift 3

for view in stackView.arrangedSubviews {
    view.removeFromSuperview()
}
目标-C

for (UIView *view in stackView.arrangedSubviews) {
    [view removeFromSuperview];
}
*注意:根据苹果公司的评论,如果你调用[stackView removeArrangedSubview:view],它实际上不会将其完全从接收器中移除。请参阅下面的引用:

-(无效)删除已安排的子视图:(UIView*)视图 “从排列的子视图列表中删除子视图,而不将其作为 接收器的子视图。 要将视图作为子视图删除,请按常规发送-removeFromSuperview; 相关UIStackView会将其从arrangedSubviews列表中删除 自动地。”


我遇到了类似的问题,在tableview单元格中添加了
arrangedSubviews

这里的技巧是,在单元格的
prepareforesue
中,使用
removeFromSuperview
从superview中删除stackView的每个子视图,然后调用
removeArrangedSubview

应该是这样的:

for view in self.views {
    radioView.removeFromSuperview()
}
self.views.removeAll()

for arrangedSubview in self.stackView.arrangedSubviews {
    self.stackView.removeArrangedSubview(arrangedSubview)
}
正如政府所说:

[code>removeArrangedSubview
]方法不会删除提供的视图 从堆栈的子视图数组;因此,视图仍然是 显示为视图层次的一部分


希望它能对某人有所帮助;)

我遇到了类似的问题,在tableview单元格中添加了
ArrangedSubview

这里的技巧是,在单元格的
prepareforesue
中,使用
removeFromSuperview
从superview中删除stackView的每个子视图,然后调用
removeArrangedSubview

应该是这样的:

for view in self.views {
    radioView.removeFromSuperview()
}
self.views.removeAll()

for arrangedSubview in self.stackView.arrangedSubviews {
    self.stackView.removeArrangedSubview(arrangedSubview)
}
正如政府所说:

[code>removeArrangedSubview
]方法不会删除提供的视图 从堆栈的子视图数组;因此,视图仍然是 显示为的一部分