开发者

adding different UILabel to each cell in UITableView

开发者 https://www.devze.com 2023-03-22 22:08 出处:网络
I\'m adding different text to each cell in UITableView. However when I do that, test is displayed in every cell except for the first one..So if theres an array with 9 numbers between 1 to 9, 1 is disp

I'm adding different text to each cell in UITableView. However when I do that, test is displayed in every cell except for the first one..So if theres an array with 9 numbers between 1 to 9, 1 is displayed in the second cell, 2 is displayed in the third cell and respectively. There's nothing shown in the first cell fromHeres the codes

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

static NSString *CellIdentifier = @"Cell";

UITa开发者_开发技巧bleViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell = [self getCellContentView:CellIdentifier];
}
if (indexPath.row == 0) {
    NSLog(@"hi");
}
//add another textfield
NSString *path = [[NSBundle mainBundle] pathForResource:@"Rankvalue" ofType:@"plist"];
NSMutableArray* rank = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSString *rankValue = [rank objectAtIndex:indexPath.row];

UILabel *rankLabel = (UILabel *)[cell viewWithTag:1];
rankLabel.text = rankValue;
[rankLabel setFont:[UIFont fontWithName:@"austin power" size:[@"40" intValue]]];

CGRect labelFrame = CGRectMake(220,70,50,40.0);
[rankLabel setFrame:labelFrame];

// Configure the cell(thumbnail).
cell.textLabel.text = [self.stars objectAtIndex:indexPath.row];
NSString *path2 = [[NSBundle mainBundle] pathForResource:@"Filename" ofType:@"plist"];
self.filename = [[NSMutableArray alloc] initWithContentsOfFile:path2];
cell.imageView.image = [UIImage imageNamed:[self.filename objectAtIndex:indexPath.row]];

//transparent cell
cell.backgroundColor = [UIColor clearColor];

[rankLabel release];
[rankValue release];

return cell;
}

And this is the code for the subview of the cell

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

CGRect CellFrame = CGRectMake(0, 0, 320, 65);
CGRect Label1Frame = CGRectMake(17,5,250,18);  

UILabel *lblTemp;


UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];  
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
[lblTemp setFont:[UIFont fontWithName:@"Arial-Bold" size:15]];
lblTemp.tag = 1;
lblTemp.backgroundColor=[UIColor clearColor];
lblTemp.numberOfLines=0;
[cell.contentView addSubview:lblTemp];    

return cell;
}


initWithFrame:reuseIdentifier: has been deprecated for initWithStyle:reuseIdentifier:

You should create one of the standard styles.

Set the values you want (font, etc.) on cell creation, not on refresh. Frame of text field is the same. And background color.

You really don't want to reload that plist (oh, there's two of them!) every time you refresh a cell! Load it once in another method and cache it as an ivar.

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

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    // create and set up all the layout attributes of the cell
    // it should be auto released
    // which should include a call like the following, or loading a cell from a nib
    // cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
    //                               reuseIdentifier:CellIdentifier];
    // [cell autorelease];
  }

  // the only thing that should happen here is setting the data values
  // into the cell!!!!!!  All these values should be loaded once and
  // kept as "model state" by the controller.  Do not create an image
  // each time through, do not load an entire plist to access one item
  // each time through

  rankLabel.text = rankValue;
  cell.textLabel.text = [self.stars objectAtIndex:indexPath.row];
  cell.imageView.image = [self imageAtRow:indexPath.row];


}

If the image your displaying is different for each cell, using imageNamed: is not appropriate. If there's 2 or 3 images that indicate type of cell that will each be used a lot, imageNamed: is likely preferable. imageWithContentsOfFile: is your other option, you'll need to build the full path


Try switching this:

static NSString *CellIdentifier = @"Cell";

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

for this:

UITableViewCell *cell=[self getCellContentView:CellIdentifier];

Not saying it will work, but just give it a shot. Also, do this inside your getCellContentView:

[lblTemp release]

Or you will have a leak.

0

精彩评论

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