objective-c - 动态填充 iPhone 中的数据

标签 objective-c ios uitableview

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [self.LoadFile count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 4;
}
 int title =0;

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    for (title = 0 ; title <= section; title++)
    {
        NSLog(@"section - %d ",title);
        NSDictionary *dict = [LoadFile objectAtIndex:title];
        NSString *titlename = [[NSString alloc]initWithFormat:@"%@",[dict valueForKey:@"name"]];
        NSLog(@"%@",titlename);
        if (section == title)
            return titlename;
    }
}

这里我使用在页面本身上创建的两个数组,在这种情况下它可以正常工作,但是对于当我需要它从数据库填充时的情况那么我应该做什么。

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    while (i < sec)
    {


    dict = [LoadFile objectAtIndex:0];
    NSString *addr = [[NSString alloc]initWithFormat:@"%@",[dict valueForKey:@"address"]];
    NSString *con = [[NSString alloc]initWithFormat:@"%@",[dict valueForKey:@"contact"]];
    NSString *sal = [[NSString alloc]initWithFormat:@"%@",[dict valueForKey:@"salary"]];
    NSString *tec = [[NSString alloc]initWithFormat:@"%@",[dict valueForKey:@"tech"]];       

    NSArray *arr = [[NSArray alloc] initWithObjects:addr,con,sal,tec,nil];
  // NSArray *arr = [[NSArray alloc] initWithObjects:i,i,i,i,nil];
      //NSArray *arr2 = [[NSArray alloc] initWithObjects:@"12",@"22",@"32",@"42",nil];
    NSLog(@"%@",arr);
    if(indexPath.section == i)
    {
        cell.textLabel.text = [arr objectAtIndex:indexPath.row];
        i++;
    }


}
    return cell;
}

    for (i = 0 ; i <= sec-1; i++)
    {
        NSLog(@"section - %d ",title);
        NSDictionary *dict = [LoadFile objectAtIndex:title];
        if (indexPath.section == 0)
        { 
            UILabel *Address = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
            Address.textColor =  [UIColor blueColor];
            Address.backgroundColor = [UIColor clearColor];
            [Address setFont:[UIFont boldSystemFontOfSize:14.0]];
            [Address setTag:i];
            Address.text = [dict objectForKey:@"address"];
            [cell.contentView addSubview:Address];
            UILabel *tech = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
            tech.textColor =  [UIColor blueColor];
            tech.backgroundColor = [UIColor clearColor];
            [tech setFont:[UIFont boldSystemFontOfSize:14.0]];
            [tech setTag:i];
            tech.text = [dict objectForKey:@"tech"];
            [cell.contentView addSubview:tech];

        }
        else if (indexPath.section == 1)
        {

            UILabel *tech = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
            tech.textColor =  [UIColor blueColor];
            tech.backgroundColor = [UIColor clearColor];
            [tech setFont:[UIFont boldSystemFontOfSize:14.0]];
            [tech setTag:i];
            tech.text = [dict objectForKey:@"tech"];
            [cell.contentView addSubview:tech];
          }
   }
   else if (indexPath.section == 0)
    {
        UILabel *contact = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
        contact.textColor =  [UIColor blueColor];
        contact.backgroundColor = [UIColor clearColor];
        [contact setFont:[UIFont boldSystemFontOfSize:14.0]];
        contact.text = [dict objectForKey:@"contact"];
        // NSLog(@"Val-%@",[dict objectForKey:@"name"]);
        [contact setTag:-4];
        [cell.contentView addSubview:contact];
    }
    else if (indexPath.section == 0)
    {
        UILabel *salary = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
        salary.textColor =  [UIColor blueColor];
        salary.backgroundColor = [UIColor clearColor];
        [salary setFont:[UIFont boldSystemFontOfSize:14.0]];
        salary.text = [dict objectForKey:@"salary"];
        // NSLog(@"Val-%@",[dict objectForKey:@"name"]);
        [salary setTag:-4];
        [cell.contentView addSubview:salary];
    }
    else
    {
        UILabel *tech = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
        tech.textColor =  [UIColor blueColor];
        tech.backgroundColor = [UIColor clearColor];
        [tech setFont:[UIFont boldSystemFontOfSize:14.0]];
        tech.text = [dict objectForKey:@"tech"];
        // NSLog(@"Val-%@",[dict objectForKey:@"name"]);
        [tech setTag:-4];
        [cell.contentView addSubview:tech];
}

我想动态地从数据库表中填充数据

最佳答案

一旦你的数组被新数据更新,你只需调用 [self.tableview reloadData];

关于objective-c - 动态填充 iPhone 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9425779/

相关文章:

ios - 使用自定义单元格在 UITableView 上进行多点触控

ios - 自定义 uitableviewcell 突出显示颜色

ios - 如何将 NSString 转换为 NSDictionary?

ios - 如何找出 UILabel wordwraps 的时间

objective-c - UIScrollView 中的 UITableView - 如何使 View 滚动,而不是 TableView 本身?

ios - 在 UITableViewCell 中让 UISwitch 获得点击然后 accessoryView 如果开关打开

iphone - 如何同步调用 NSStream

ios - 如何以编程方式更改 Swift 中导航栏的高度?

iphone - 如何在 UITableViewCell 中动态调整两个文本标签的宽度?

ios - 如何让 NSTimer 显示月份 : Weeks : Days