UITableview从头开始

时间:2014-02-13 10:46:45

标签: ios objective-c uitableview

我有UITableView我需要添加文字和图片 - 简单。 文本来自数据库中的一个表,图像来自另一个表。这些表与ID列链接。

文本来自SQL命令块,它还带来了ID。然后使用Custom API查询ID,并返回图像的URL。

现在经历了这么多麻烦之后。我写得很简单。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    ImageURL = [[NSMutableArray alloc] initWithObjects:@"http://calcuttans.com/palki/wp-content/uploads/2009/02/kidscover-small.png",@"http://outtonightapp.com/uploads/wether2.jpg",@"http://outtonightapp.com/uploads/wether3.jpg",nil];
    notification = [[NSMutableArray alloc] initWithObjects:@"wow",@"shit",@"tito", nil];  
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [notification count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    // putting text in rows by indexpath.row
    cell.textLabel.text = notification[indexPath.row];

    // putting an image on a row

    switch (indexPath.row) {
        case 0:
            [cell.imageView setImageWithURL:[NSURL URLWithString:[ImageURL objectAtIndex:(0)]] placeholderImage:[UIImage imageNamed:@"greybox40.png"]];
            break;
        case 1:
            [cell.imageView setImageWithURL:[NSURL URLWithString:[ImageURL objectAtIndex:(1)]] placeholderImage:[UIImage imageNamed:@"greybox40.png"]];
            break;
        case 2:
            [cell.imageView setImageWithURL:[NSURL URLWithString:[ImageURL objectAtIndex:(2)]] placeholderImage:[UIImage imageNamed:@"greybox40.png"]];
            break;
    }
    return cell;
}

@end

现在我需要删除数组并从SQL获取数据并放入数组中,但是当我在过去完成此操作时,由于程序流程,它们还没有进入。那么如何最好地将它们放入数组中。我应该为文本创建.h.m文件,为图片创建一个文件,然后从viewDidLoad运行它们。我不想处于我拥有所有数据的位置,并且它不再显示在tableview中。目前此代码有效!

谢谢

杰森

3 个答案:

答案 0 :(得分:0)

[view didLoad]上的应用开始时显示一些活动指标,而数据正在加载, 在获得notification数组调用[_yourTableViewName reloadData];中的所有数据并停止活动指示符

答案 1 :(得分:0)

在我看来,你真的不想'删除数组'......

tableView本质上是一个用于表示数组的ui设备,或者在分组表的情况下,是一个数组数组。每个单元格应该代表一个数组中的成员,以便屏幕上的内容与数据模型匹配。在你的例子中,我将看一个NSDictionaries的nsarray,每个都有对象的匹配键,如NSStrings和图像等,或者你自己的自定义类的对象数组,它们具有你可以在检索数据时设置的属性,图像文件名称,字符串等模型应始终与屏幕上绘制的内容相匹配

答案 2 :(得分:0)

  

文本来自SQL命令块,它也带来了   ID。

因此,将dictionary形成为ID作为键,将文本作为值形成。在numberOfRowsInSectioncellForRowAtIndexPath中返回字典计数,从ID创建网址并查询图片。

例如你的dict可能有以下结构:

{
   "1" : "wow",
   "2" : "shit"
}

现在将numberOfRowsInSection中的字典计数返回为

return [dict count];

现在在cellForRowAtIndexPath,获取ID

NSString *id= [[actorsDictionary allKeys] objectAtIndex:indexPath.row];
//Create your custom api based on id and call to get image
//Note: You can try out AsyncImageView if you want

这只是满足您要求的另一种方式。希望它有所帮助。

相关问题