开发者

Objective-C: First item of NSArray sometimes is missing

I have and NSArray with 5 string elements, populating an UITableView of my iPhone App.

Everything works fine, but sometimes the first string of my array is missing and I can't figure out why.

Here is my code:

arrayList = [[NSArray alloc] initWithObjects: @"My First String", 
                                              @"My Second String",
                                              @"My Third String",
                                              @"My Forth String",
                                              @"My Fifth Sring",
                                              nil];

The element that sometimes just doesn't show up is My First String

Actually I'm using this sample code from Cocoa With Love. Thanks guys for helping me!

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

#if USE_CUSTOM_DRAWING
    const NSInteger TOP_LABEL_TAG = 1001;
//  const NSInteger BOTTOM_LABEL_TAG = 1002;
    UILabel *topLabel;
//  UILabel *bottomLabel;
#endif

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    //
    // Create the cell.
    //
    cell =
    [[[UITableViewCell alloc]
      initWithFrame:CGRectZero
      reuseIdentifier:CellIdentifier]
     autorelease];

#if USE_CUSTOM_DRAWING

    UIImage *indicatorImage = [UIImage imageNamed:@"image1.png"];
    cell.accessoryView =
    [[[UIImageView alloc]
      initWithImage:indicatorImage]
     autorelease];

    const CGFloat LABEL_HEIGHT = 22;
    UIImage *image = [UIImage imageNamed:@"image2.png"];

    //
    // Create the label for the top row of text
    //
    topLabel =
    [[[UILabel alloc]
      initWithFrame:
      CGRectMake(
                 image.size.width + 2.0 * cell.indentationWidth,
                 0.5 * (aTableView.rowHeight - 1.3 * LABEL_HEIGHT),
                 aTableView.bounds.size.width -
                 image.size.width - 4.0 * cell.indentationWidth
                 - indicatorImage.size.width,
                 LABEL_HEIGHT)]
     autorelease];
    [cell.contentView addSubview:topLabel];

    //
    // Configure the properties for the text that are the same on every row
    //
    topLabel.tag = TOP_LABEL_TAG;
    topLabel.backgroundColor = [UIColor clearColor];


     topLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
     topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];


    topLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
    topLabel.textAlignment = UITextAlignmentCenter; 

    /*

    //
    // Create the label for the bottom row of text
    //
    bottomLabel =
    [[[UILabel alloc]
      initWithFrame:
      CGRectMake(
                 image.size.width + 2.0 * cell.indentationWidth,
                 0.5 * (aTableView.rowHeight - 2 * LABEL_HEIGHT) + LABEL_HEIGHT,
                 aTableView.bounds.size.width -
                 image.size.width - 4.0 * cell.indentationWidth
                 - indicatorImage.size.width,
                 LABEL_HEIGHT)]
     autorelease];
    [cell.contentView addSubview:bottomLabel];

    //
    // Configure the properties for the text that are the same on every row
    //
    bottomLabel.tag = BOTTOM_LABEL_TAG;
    bottomLabel.backgroundColor = [UIColor clearColor];
    bottomLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
    bottomLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
    bottomLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize] - 2];

    */



    //
    // Create a background image view.
    //
    cell.backgroundView =
    [[[UIImageView alloc] init] autorelease];
    cell.selectedBackgroundView =
    [[[UIImageView alloc] init] autorelease];
#endif
}

#if USE_CUSTOM_DRAWING
else
{
    topLabel = (UILabel *)[cell viewWithTag:TOP_LABEL_TAG];
//  bottomLabel = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG];
}

topLabel.text = [arrayList objectAtIndex:indexPath.row]; 
//bottomLabel.text = [NSString stringWithFormat:@"Some other information.", [indexPath row]];

//  [cell LabelText:[arrayList objectAtIndex:indexPath.row]];


//
// Set the background and selected background images for the text.
// Since we will round the corners at the top and bottom of sections, we
// need to conditionally choose the images based on the row index and the
// number of rows in the section.
//
UIImage *rowBackground;
UIImage *selectionBackground;
NSInteger sectionRows = [aTableView numberOfRowsInSection:[indexPath section]];
NSInteger row = [indexPath row];
if (row == 0 && row == sectionRows - 1)
{
    rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"];
    selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected_copy.png"];
}
else if (row == 0)
{
    rowBackground = [UIImage imageNamed:@"topRow.png"];
    selectionBackground = [UIImage imageNamed:@"topRowSelected_copy.png"];
}
else if (row == sectionRows - 1)
{
    rowBackground = [UIImage imageNamed:@"bottomRow.png"];
    selectionBackground = [UIImage imageNamed:@"bottomRowSelected_copy.png"];
}
else
{
    rowBackground = [UIImage imageNamed:@"middleRow.png"];
    selectionBackground = [UIImage imageNamed:@"middleRowSelected_copy.png"];
}
((UIImageView *)cell.backgroundView).image = rowBackground;
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;



if (row == 0)
{
    cell.imageView.image = [UIImage imageNamed:@"pic1.png"];
}
else if (row == 1)
{
    cell.imageView.image = [UIImage imageNamed:@"pic2.png"];
}
else if (row == 2)
{
    cell.imageView.image = [UIImage imageNamed:@"pic3.png"];
}

else if (row == 3)
{
    cell.imageView.image = [UIImage imageNamed:@"pic4.png"];
}

else if (row == 4)
{
    cell.imageView.image= [UIImage imageNamed:@"pic5.png"];
}

#else

[cell LabelText:[arrayList objectAtIndex:indexP开发者_如何学编程ath.row]];

//cell.text = [NSString stringWithFormat:@"Cell at row %ld.", [indexPath row]];
#endif

return cell;
}


The issue you are probably having is accessing object 1 as index 1... Arrays in Objective-C are the same as most other C structured languages. Indexed starting at 0.

Put this code in.

for (int i = 0; i < [arrayList count]; i++)
{
    NSLog(@"Item %d = %@", i, [arrayList objectAtIndex:i]);
}

Chances are good that this works fine.

If that is not the case the I would recommend restoring your iphone to factory default as either the operating system is malfunctioning, or you have other rogue processes that are corrupting memory.

To debug the code above. As you are grabbing the items out of the array I would put in an NSLog()

NSString *objectAtIndex = [arrayList objectAtIndex:indexPath.row];
NSLog(@"Row: %d, Object: %@, Count: %d",indexPath.row, objectAtIndex, [arrayList count]);
[cell LabelText:[arrayList objectAtIndex:indexPath.row]];


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [arrayList objectAtIndex:indexPath.row];
return cell;
}

Are you doing something similar to this for populating table view


NSMutableArray *ArrayList=[[NSMutableArray   alloc]initWithObjects: @"My First String", 
                                              @"My Second String",
                                              @"My Third String",
                                              @"My Forth String",
                                              @"My Fifth Sring",
                                              nil];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜