如何在Objective C中自动滚动自定义表格视图单元格

Posted

技术标签:

【中文标题】如何在Objective C中自动滚动自定义表格视图单元格【英文标题】:How to autoscroll Custom Table View cell in Objective C 【发布时间】:2017-01-10 05:23:53 【问题描述】:

我是 ios 新手,我遇到了关于自动滚动表格视图单元格的问题。 我正在使用这样的代码

- (void)viewDidAppear:(BOOL)animated

  [table setContentOffset:CGPointMake(0.0, table.contentSize.height - table.bounds.size.height)
                 animated:YES]; 

  [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[headarray count]-1 inSection:0]                                                
                   atScrollPosition:UITableViewScrollPositionBottom
                           animated:YES];

但它向我展示了我在这段代码中做错了什么。在此先感谢!

我在 connectionDidFinishLoading 方法中尝试了这样的代码

if ([headarray count] != 0) 

            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[headarray count]-1 inSection:0];
            [Newstable scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
         

【问题讨论】:

您的代码绝对正确,但请检查此 [musicGenre count]-1 inSection:0] ,它返回的内容,检查其值和类。 @muja 试试我的代码,然后删除你的 [table setContentOffset:CGPointMake(0.0, table.contentSize.height - table.bounds.size.height) animated:YES];这一行 @Muju 你想像聊天一样滚动到表格的最后一行吗? 你的数据源数组是哪个? headarraymusicGenre ? @PiyushPatel Head 数组是我的数据源数组。 【参考方案1】:

查看此链接https://github.com/danielamitay/DAAutoScroll。您可以使用这些类来自动滚动您的表格视图。 1. 导入类

导入“DAAutoScroll.h”

- (void)viewDidLoad 
    [self performSelector:@selector(scrollDown) withObject:self afterDelay:3.0];


-(void)scrollDown

    yourtableview.scrollPointsPerSecond = 20.0f;
    [yourtableview startScrolling];


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

    if (!decelerate) 
        [yourtableview startScrolling];
    


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

    [yourtableview startScrolling];

【讨论】:

在演示中它工作得很好,但是当我在项目中使用它时它不起作用。 @Muju 当您尝试在项目中使用它时有什么问题? @Muju Thant 表示您不知道如何在您的项目中实现它,因为演示不可能完美运行,但在您的项目中却无法运行。 @PiyushPatel 我使用了自定义表格视图单元格,并在我的项目中实现了它。是的,可能是我在实施时做错了什么。 @Muju : 不管你是否使用自定义单元格。【参考方案2】:
[tableviewname setContentOffset:CGPointMake(0, ([arrayofcontact count]-10)*cellheight) animated:YES];

【讨论】:

这段代码有什么作用,为什么能解决问题?

以上是关于如何在Objective C中自动滚动自定义表格视图单元格的主要内容,如果未能解决你的问题,请参考以下文章

Objective -C XCTest:如何在表格单元格中一直向上滑动

如何从持久存储中获取数据并显示在自定义表格视单元上?有没有示例代码?

如何根据objective-c中的滚动方向创建一个从UINavBar后面向下或向上滑动的自定义UIView?

如何在快速向上和向下滚动表格时停止自动重新加载表格视图? [关闭]

如何使用Objective C以2秒的间隔一张一张地自动滚动图像?

Objective c - 表格视图内的按钮防止表格滚动