为什么我的tableview很慢? [英] Why my tableview is slow?

查看:65
本文介绍了为什么我的tableview很慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些NSArrays加载一个tableview,该单元格包含两个标签和一个加载了URL图像的背景图像视图。问题是tableview的滚动速度很慢,就像冻结一样....我认为这是因为Imageview但我该怎么做???



<继承了我的一些代码,你可以看到:

   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection :( NSInteger)部分
{

//返回部分中的行数。


return _Title.count;


}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @Cell;
MyCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

int row = [indexPath row];

cell.TitleLabel.text = _Title [row];
cell.DescriptionLabel.text = _content [row];
cell.CellImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_ImageUrl [row]]]];
cell.CellImageView.contentMode = UIViewContentModeTop;
cell.CellImageView.contentMode = UIContentSizeCategoryMedium;

cell.backgroundColor = [UIColor clearColor];





返回单元;
}

有关额外信息,此tableview提供了一个具有相同信息的Detailviewcontroller。

解决方案

您使用以下代码行阻止主线程:

  cell.CellImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_ImageUrl [row]]]]; 

我的建议是使用 AFNetworking 并用以下内容替换代码:

  [cell.cellImageView setImageWithURL:[ NSURL URLWithString:@http://example.com/image.png]]; 

此外,您的指针应以小写字母开头。例如,CellImageView应该是cellImageView。


im making a tableview loaded with some NSArrays, the cell contains two labels and a background image view loaded with a URL image. The problem is that the scrolling of the tableview is slow, its like freeze or something.... i think that is because of the Imageview but what can i do???

heres some of my code so you can see:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.


    return _Title.count;


}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    int row = [indexPath row];

    cell.TitleLabel.text = _Title[row];
    cell.DescriptionLabel.text = _content[row];
    cell.CellImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_ImageUrl[row]]]];
    cell.CellImageView.contentMode = UIViewContentModeTop;
    cell.CellImageView.contentMode = UIContentSizeCategoryMedium;

    cell.backgroundColor = [UIColor clearColor];





    return cell;
}

For extra info this tableview presents a Detailviewcontroller with this same info.

解决方案

You are blocking the main thread with the following line of code:

cell.CellImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_ImageUrl[row]]]];

My suggestion would be to use AFNetworking and replace the code with the following:

[cell.cellImageView setImageWithURL:[NSURL URLWithString:@"http://example.com/image.png"]];

Also, your pointers should start with a lower case letter. For instance, CellImageView should be cellImageView.

这篇关于为什么我的tableview很慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆