0

我想将我的应用程序连接到数据库并将其显示在标签中。我可以将我的应用程序连接到数据库并在 UITableView 中显示它。

这是我到目前为止所拥有的:

ViewController.h

#import <UIKit/UIKit.h>

@interface CartHistoryViewController : UITableViewController
{
    NSMutableArray *arrayDataFromServer;
}


@end

视图控制器.m

- (void)viewDidLoad
{
    [super viewDidLoad];



    NSString *strURL = [NSString stringWithFormat:@"http://localhost:8888/CartGet.php?  choice=history"];
NSArray *arrayImagesNames = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]];

strURL = @"http://localhost:8888/CartGet.php?choice=historydate";
NSArray *arrayImagesPaths = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]];

// store the result in arrayDataFromServer
arrayDataFromServer = [[NSMutableArray alloc]init];

NSEnumerator *enumForNames = [arrayImagesNames objectEnumerator];
NSEnumerator *enumForPahts = [arrayImagesPaths objectEnumerator];

id objName, objPath;

while ( objName = [enumForNames nextObject]) {
    objPath = [enumForPahts nextObject];
    [arrayDataFromServer addObject:[NSDictionary dictionaryWithObjectsAndKeys:objName, @"name", objPath, @"path", nil]];
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


static NSString *CellIdentifier = @"Cell";

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


cell.textLabel.text = [[arrayDataFromServer objectAtIndex:indexPath.row] objectForKey:@"name"];
[cell.textLabel setFont:[UIFont systemFontOfSize:20]];
 cell.detailTextLabel.text = [[arrayDataFromServer objectAtIndex:indexPath.row] objectForKey:@"path"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];


  NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[[arrayDataFromServer objectAtIndex:indexPath.row] objectForKey:@"path"]]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [UIImage imageWithData:data];


  cell.imageView.image = img;

return cell;
}

我希望能够在 Label 和 ImageView 而不是单元格中显示它。请帮忙。

4

1 回答 1

0

转到您的 Interface Builder 选择您的表格视图并制作它Grouped TableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath在您的方法之前添加以下代码

#pragma mark - Table view data source

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [arrayDataFromServer count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ 
   return 0;
}
// set header height of gropued tableview
-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section 
{
  return 120;//change this value if it is too big
}
//set header section labels
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
      NSString * subject=[[arrayDataFromServer objectAtIndex:section] objectForKey:@"name"];//this line may give you error because of section easy to correct
     UILabel *subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 30, 100, 100)];
     subjectLabel.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
      subjectLabel.font = [UIFont fontWithName:@"Arial" size:25];
      subjectLabel.text = subject;
      subjectLabel.backgroundColor = [UIColor clearColor];
      [subjectLabel sizeToFit];

      // if you want to add image view create an imageview programatically here 

// NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[[arrayDataFromServer objectAtIndex:section] objectForKey:@"path"]]];
//NSData *data = [NSData dataWithContentsOfURL:url];
//UIImage *img = [UIImage imageWithData:data];
// UIImageView *brickAnim = [[UIImageView alloc] initWithImage:img];



       // Create header view and add label as a subview choose coordinates wisely
       UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 120, 100, G00)];


        [view addSubview:subjectLabel];
        //[view addSubview:brickAnim];

        return view;
}
于 2012-12-21T16:28:11.297 回答