0

アプリをデータベースに接続して、ラベルに表示したいと思います。アプリをデータベースに接続して、UITableViewに表示することができます。

これは私がこれまでに持っているものです:

Viewontroller.h

#import <UIKit/UIKit.h>

@interface CartHistoryViewController : UITableViewController
{
    NSMutableArray *arrayDataFromServer;
}


@end

ViewController.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;
}

セルではなく、ラベルと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 に答える