0

私のテーブルビューセルでは、配列要素の内容を単一のセルに表示する必要があり、新しい内容を挿入した場合、以前の内容は上書きされません。以前の内容と新しい内容は順番に表示されます。私のコード

- (void)viewDidLoad
{
[super viewDidLoad];




NSMutableArray *arrMain=[NSMutableArray arrayWithObjects: cnfqty, cnftitle, cnfrate, nil];

finarray=[[NSMutableArray alloc]init];
[finarray addObjectsFromArray:arrMain];
   NSLog(@"%@",finarray);


}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }

 #pragma mark - Table view data source

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {

// Return the number of sections.
return 1;
}

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {

return 150.0;

 }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
 // Return the number of rows in the section.
return ( [finarray count] / 3 ); 
}


 - (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

UILabel *tempLabel;
//UIImage *tempImage;

/* Create UI elements - Customize as you want your text to appear */

CGRect myFirstLabel = CGRectMake (65, 10, 200, 15);
CGRect mySecondLabel = CGRectMake (65, 25, 200, 15);
CGRect myThirdLabel = CGRectMake (65, 40, 200, 15);
 //  CGRect myImage = CGRectMake (10, 10, 50, 50);

//Initialize first label
tempLabel = [[UILabel alloc] initWithFrame: myFirstLabel];
tempLabel.tag = 1;
tempLabel.font = [UIFont boldSystemFontOfSize:12];
tempLabel.textColor = [UIColor lightGrayColor];
[cell.contentView addSubview: tempLabel];

//Initialize second label
tempLabel = [[UILabel alloc] initWithFrame: mySecondLabel];
tempLabel.tag = 2;
tempLabel.font = [UIFont boldSystemFontOfSize:12];
tempLabel.textColor = [UIColor lightGrayColor];
[cell.contentView addSubview: tempLabel];

//Initialize third label
tempLabel = [[UILabel alloc] initWithFrame: myThirdLabel];
tempLabel.tag = 3;
tempLabel.font = [UIFont boldSystemFontOfSize:12];
tempLabel.textColor = [UIColor lightGrayColor];
[cell.contentView addSubview: tempLabel];

//Initialize your picture
//imgTemp = [[UIImageView alloc] initWithFrame: myImage];
//imgTemp.tag = 4;
//[cell.contentView addSubview: tempImage];

return cell;
 }

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [self getCellContentView: CellIdentifier];
}

//This will create the actual UI elements
UILabel *tempLabel1 = (UILabel *)[cell viewWithTag: 1];
UILabel *tempLabel2 = (UILabel *)[cell viewWithTag: 2];
UILabel *tempLabel3 = (UILabel *)[cell viewWithTag: 3];
//UIImageView *tempImage = (UIImageView *)[cell viewWithTag: 4];

// Populate the labels
NSString *firstText = [NSString stringWithString: [finarray objectAtIndex: 3*(indexPath.row)]];
NSString *secondText = [NSString stringWithString: [finarray objectAtIndex: 3*(indexPath.row) + 1]];
NSString *thirdText = [NSString stringWithString: [finarray objectAtIndex: 3*(indexPath.row) + 2]];

// Populate the picture
//UIImage *picture = [[UIImage alloc] initWithData:theObjectToPutInCell.picture];

tempLabel1.text = firstText;
tempLabel2.text = secondText;
tempLabel3.text = thirdText;

 // tempImage.image = picture;

return cell;  


}
}

@end

私のコードで直面している問題は、各文字列が各セルに表示され、新しい文字列を挿入すると以前の内容が消えて新しい内容だけが表示されることです。

この質問について明確に理解するために、オンライン ショッピングの「カートに追加」サービスを実装しようとしています。コンセプトに従って、さまざまな製品からアイテムを追加する必要があり、製品情報を保存し、詳細をテーブル ビューに表示する必要があります。 .しかし、私はそれを取得していません..

親切にガイドしてください..よろしくお願いします..

私の出力はこの最初の画像のようです

ここに画像の説明を入力

しかし、Androidで行った2番目の画像として表示する必要があります.. ここに画像の説明を入力

4

3 に答える 3

3

ここにあなたが試すことができるものがあります:

次のような関数を作成して、必要なすべてのグラフィック要素を作成します。

// Cell appearence editing
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    UILabel *tempLabel;
    UIImage *tempImage;

    /* Create UI elements - Customize as you want your text to appear */

    CGRect myFirstLabel = CGRectMake (65, 10, 200, 15);
    CGRect mySecondLabel = CGRectMake (65, 25, 200, 15);
    CGRect myThirdLabel = CGRectMake (65, 40, 200, 15);
    CGRect myImage = CGRectMake (10, 10, 50, 50);

    //Initialize first label
    tempLabel = [[UILabel alloc] initWithFrame: myFirstLabel];
    tempLabel.tag = 1;
    tempLabel.font = [UIFont boldSystemFontOfSize:12];
    tempLabel.textColor = [UIColor lightGrayColor];
    [cell.contentView addSubview: tempLabel];

    //Initialize second label
    tempLabel = [[UILabel alloc] initWithFrame: mySecondLabel];
    tempLabel.tag = 2;
    tempLabel.font = [UIFont boldSystemFontOfSize:12];
    tempLabel.textColor = [UIColor lightGrayColor];
    [cell.contentView addSubview: tempLabel];

    //Initialize third label
    tempLabel = [[UILabel alloc] initWithFrame: mySecondLabel];
    tempLabel.tag = 3;
    tempLabel.font = [UIFont boldSystemFontOfSize:12];
    tempLabel.textColor = [UIColor lightGrayColor];
    [cell.contentView addSubview: tempLabel];

    //Initialize your picture
    imgTemp = [[UIImageView alloc] initWithFrame: myImage];
    imgTemp.tag = 4;
    [cell.contentView addSubview: tempImage];

    return cell;
}

次に、cellForRowAtIndexPath で「デザイン関数」を呼び出し、コンテンツを入力します。

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

    static NSString *CellIdentifier = @"aCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [self getCellContentView: CellIdentifier];
    }

    //This will create the actual UI elements
    UILabel *tempLabel1 = (UILabel *)[cell viewWithTag: 1];
    UILabel *tempLabel2 = (UILabel *)[cell viewWithTag: 2];
    UILabel *tempLabel3 = (UILabel *)[cell viewWithTag: 3];
    UIImageView *tempImage = (UIImageView *)[cell viewWithTag: 4];

    // Populate the labels
    NSString *firstText = [NSString stringWithString: [finarray stringAtIndex: 3*(indexPath.row)]];
    NSString *secondText = [NSString stringWithString: [finarray stringAtIndex: 3*(indexPath.row) + 1]];
    NSString *thirdText = [NSString stringWithString: [finarray stringAtIndex: 3*(indexPath.row) + 2]];

    // Populate the picture
    UIImage *picture = [[UIImage alloc] initWithData:theObjectToPutInCell.picture];

    tempLabel1.text = firstText;
    tempLabel2.text = secondText;
    tempLabel3.text = thirdText;

    tempImage.image = picture;

    return cell;

}

それが役立つかどうか教えてください!

于 2012-07-26T13:17:20.393 に答える
1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

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

    // Return the number of rows in the section.
    return [itemsData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // static NSString *CellIdentifier = @"Cell";
    NSString *CellIdentifier = [NSString stringWithFormat:@"%d", indexPath.row] ;
    UILabel *lblTitle = nil;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;




        lblTitle =[[UILabel alloc]initWithFrame:CGRectMake(5, 15, 110, 44)];
        lblTitle.backgroundColor = [UIColor clearColor];
        lblTitle.textColor = [UIColor whiteColor];
        lblTitle.font = [UIFont systemFontOfSize:11.0];
        lblTitle.tag = 1;
        lblTitle.numberOfLines = 0;


        UILabel *lblType =[[UILabel alloc]initWithFrame:CGRectMake(125, 0, 75, 44)];
        lblType.backgroundColor = [UIColor clearColor];
        lblType.textColor = [UIColor whiteColor];
        lblType.font = [UIFont systemFontOfSize:11.0];
        lblType.tag = 2;

        UILabel *lblDate =[[UILabel alloc]initWithFrame:CGRectMake(200, 0, 60, 44)];
        lblDate.backgroundColor = [UIColor clearColor];
        lblDate.textColor = [UIColor whiteColor];
        lblDate.font = [UIFont systemFontOfSize:11.0];
        lblDate.tag = 3;

        UILabel *lblTime =[[UILabel alloc]initWithFrame:CGRectMake(265, 0, 50, 44)];
        lblTime.backgroundColor = [UIColor clearColor];
        lblTime.textColor = [UIColor whiteColor];
        lblTime.font = [UIFont systemFontOfSize:11.0];
        lblTime.tag = 4;

        [tableView beginUpdates];
        [tableView endUpdates]; 
        [cell.contentView addSubview:lblTitle];
        [cell.contentView addSubview:lblType];
        [cell.contentView addSubview:lblDate];
        [cell.contentView addSubview:lblTime];

    }

NSString *text = [NSString stringWithFormat:@"%@,%@,%@",[[itemsData objectAtIndex:indexPath.row] objectForKey:@"address"],[[itemsData objectAtIndex:indexPath.row] objectForKey:@"title"],[[itemsData objectAtIndex:indexPath.row] objectForKey:@"zipcode"]];


    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeCharacterWrap];

    if (!lblTitle)
        lblTitle = (UILabel*)[cell viewWithTag:1];

    [lblTitle setText:text];
    [lblTitle setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];


    UILabel *lblType = (UILabel *) [cell.contentView viewWithTag:2];
    lblType.text =[[itemsData objectAtIndex:indexPath.row] objectForKey:@"type"];

    UILabel *lblDate = (UILabel *) [cell.contentView viewWithTag:3];
    lblDate.text =[[itemsData objectAtIndex:indexPath.row] objectForKey:@"date"];

    UILabel *lblTime = (UILabel *) [cell.contentView viewWithTag:4];
    lblTime.text =[[itemsData objectAtIndex:indexPath.row] objectForKey:@"time"];

    return cell;
}
于 2012-07-30T12:51:27.317 に答える
1

最後に、次のコードで取得しました

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

static NSString *CellIdentifier = @"aCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *label1=[[UILabel alloc]init];
label1.frame=.....;
................
[cell.contentView addSubView: label1];

UILabel *label2=[[UILabel alloc]init];
label2.frame=.....;
................
[cell.contentView addSubView: label2];

UILabel *label3=[[UILabel alloc]init];
label3.frame=.....;
................
[cell.contentView addSubView: label3];
}

ラベルへのテキストは、このように個々の配列要素から割り当てられます

NSMutableArray *label1Array=[NSMutableArray arrayWithObjects:@"",@"",nil];
NSMutableArray *label2Array=[NSMutableArray arrayWithObjects:@"",@"",nil];
NSMutableArray *label3Array=[NSMutableArray arrayWithObjects:@"",@"",nil];
于 2013-02-09T11:59:10.970 に答える