Objective-C:有时缺少NSArray的第一项

时间:2011-05-09 23:53:27

标签: iphone objective-c cocoa-touch nsstring nsarray

我有和5个字符串元素的NSArray,填充我的iPhone应用程序的UITableView。

一切正常,但有时我的数组的第一个字符串丢失,我无法弄清楚原因。

这是我的代码:

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

有时不会显示的元素是我的第一个字符串

实际上我正在使用Cocoa With Love的示例代码。谢谢你帮助我!

- (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:indexPath.row]];

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

return cell;
}

3 个答案:

答案 0 :(得分:5)

您可能遇到的问题是将对象1作为索引1访问... Objective-C中的数组与大多数其他C结构语言相同。从0开始索引。

将此代码放入。

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

这很好,这很好。

如果不是这种情况,我建议将iphone恢复为出厂默认值,因为操作系统出现故障,或者您有其他恶意进程损坏内存。

调试上面的代码。当你抓住数组中的项目时,我会放入一个NSLog()

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

答案 1 :(得分:0)

- (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;
}

您是否正在为填充表格视图做类似的事情

答案 2 :(得分:-2)

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