开发者

Adding a row of icons to a table cell

开发者 https://www.devze.com 2022-12-20 15:50 出处:网络
Okay I am just totally too close to this I think.I have a tableview.In that table view everything is working except section 2 row 0.

Okay I am just totally too close to this I think. I have a tableview. In that table view everything is working except section 2 row 0.

AND -- before anyone asks...I know I am not using the *imgWordView (in the code below) and that is why the icons are not showing but i do not know where to add it to the subview so i can place it at coordinates in the cell... :)

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
  if (indexPath.section == 2 && indexPath.row == 0)
  {
   cell = [self getCellContentViewForIconRow:CellIdentifier];
   cell.backgroundColor = [UIColor grayColor];

  }
  else
        {
   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  }
 }
}

The above code (except for my addition of of my section == 2 && indexPath.row == 0) is normal and generic..

getCellContentViewForIconRow is defined here:

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

 ResortsListViewController *resortsListViewController = [[ResortsListViewController alloc] init];
 NSMutableArray *categoryArray = [resortsListViewController getCategoriesForLocation:[[locationData objectForKey:@"theid"]intValue]];
 [resortsListViewController release];

 CGRect CellFrame = CGRectMake(0, 0, 260, 60);
 UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
 cell.contentView.backgroundColor = [UIColor grayColor];
 开发者_高级运维int x = 10;
 int y = 10;

 for (NSMutableDictionary *cat_object in categoryArray) {

  CGRect Label1Frame = CGRectMake(x, y, x + 40, 40);
  UILabel *lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
  lblTemp.tag = x + 1;

  NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"alcohol.png"];
  UIImage *imgWord = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease];

  UIImageView *imgWordView = [[[UIImageView alloc] initWithImage:imgWord] autorelease];

  [cell.contentView addSubview:lblTemp];
  [lblTemp release];

  x = x + 30;
 }

 return cell;
        }

Okay, so

ResortsListViewController *resortsListViewController = [[ResortsListViewController alloc] init];
NSMutableArray *categoryArray = [resortsListViewController getCategoriesForLocation:[[locationData objectForKey:@"theid"]intValue]];
[resortsListViewController release];

gets back a group of categories that I would use to build the filenames for the PNG images. Everything is okay so far. Then I am looping through the array to build the places for the images in the cell.

I am expecting no more than 5 images...at 30x30 each. so, to test, I just used the filename "alcohol.png" which exists. I want all 5 images to be displayed in a row inside the cell

I already do this:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
 if (indexPath.section == 2 && indexPath.row == 0)
        {
        return 60;
 }
}

But, all I see is my table cell (in gray like it is supposed to be) with a LONG white box inside it.

So, two questions.

1: The box (i am assuming it is the CGRect CellFrame = CGRectMake(0, 0, 260, 60);) in white. I need to know how to change that to gray...just like my cell background.

The big reason is that my icons are white...and i cannot tell if the icons are visible or not.

If the only problem is that this white CGRECT is hiding my icons, then all is well.

Otherwise we come to #2:

2: Is this the best way to put a group of icons within a cell row when the icon names are inside an array?

If not, how?

So, How do i make the CGRect gray & how do i get the 5 icons to appear horizontally in the cell?

Thanks in advance for your help!


The big long white box is your labels you are making and adding to the contentView. The UILabels are opaque with a white background. Do the following:

lblTemp.backgroundColor = [UIColor clearColor];

This will get rid of the white box so you can see what is behind your labels.

The other issue is that you you are adding your x + 40 on every iteration of your loop. I would think you would want the width to be a constant value and not adding your X coord + 40 each time, this will end up making a pretty wide set of labels across your cell.


Okay, found out how to do this elegantly and with a flair. I am voting for @bstahlhood's answer as the answer as it is partially what I needed to know..however, this subroutine in place of the one I submitted above is how I fully fixed it.

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

    ResortsListViewController *resortsListViewController = [[ResortsListViewController alloc] init];
    NSMutableArray *categoryArray = [resortsListViewController getCategoriesForLocation:[[locationData objectForKey:@"theid"]intValue]];
    [resortsListViewController release];

    MainAppDelegate *mainAppDelegate = [[MainAppDelegate alloc] init];

    CGRect CellFrame = CGRectMake(10, 10, 100, 40);
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];

    int x = 10;
    int y = 10;
    int ctr = 0;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    for (NSString *category_name in categoryArray) {
        if (ctr <= 7)
        {
            BOOL valid_for_user = [mainAppDelegate validate_category_for_users_level:category_name];
            if (valid_for_user)
            {
                NSMutableString *subt = [[[NSMutableString alloc]init]autorelease];
                [subt appendString: category_name];
                [subt appendString: @".png"];

                NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithString:subt]];
                BOOL success = [fileManager fileExistsAtPath:filePath];
                if (success)
                {
                    UIImage *imgWord = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease];
                    UIImageView *imgWordView = [[[UIImageView alloc] initWithImage:imgWord] autorelease];

                    CGRect Label1Frame = CGRectMake(x, y, 30, 30);
                    UILabel *lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
                    lblTemp.tag = x + 1;
                    // BEGIN FIX TO MY PROBLEM: thanks @bstahlhood
                    lblTemp.backgroundColor = [UIColor clearColor];
                    // END FIX TO MY PROBLEM
                    [lblTemp addSubview:imgWordView];

                    [cell.contentView addSubview:lblTemp];
                    [lblTemp release];

                    x = x + 40;
                    ctr += 1;
                }
            }
        }


    }
    [mainAppDelegate release];
    return cell;
}

As you see I also changed the CGRECT inside my loop to fix the horizontal spacing:

CGRect Label1Frame = CGRectMake(x, y, 30, 30);

as @bstahlhood suggested. I had forgotten CGRrect commands handle the startpoints and width instead of startpoints and endpoints inside the cell area. that is why I thought I had to use x+40. As stated, it is now corrected.

I also changed this the heightForRowAtIndexPath to better balance the look of a 30x30 pixel image. The label above starts my subview 10px across and 10px down from the inside of the cell, so it just makes sense to ensure that the cell is 50px vertically and not 60px so there is exactly 10px on top and 10px on bottom of the icon.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
 if (indexPath.section == 2 && indexPath.row == 0)
        {
        return 50;
 }
}

So, even though I solved my own problem prior to the posting by @bstahlhood, I am giving him the vote for the correct answer as it was correct. I also wanted to point out that I wish more people would post fixed code after their issue was corrected so that others having the same issue would know what (exactly) they did. That is why I posted this code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号