Objective-C:NSArray 的第一项有时会丢失

发布于 2024-11-05 21:26:12 字数 5967 浏览 5 评论 0原文

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

一切工作正常,但有时我的数组的第一个字符串丢失,我不明白为什么。

这是我的代码:

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

有时不显示的元素是 My First String

实际上,我正在使用 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;
}

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

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

return cell;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

极度宠爱 2024-11-12 21:26:12

您可能遇到的问题是将对象 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]];

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]];
街角卖回忆 2024-11-12 21:26:12
- (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;
}

您是否正在执行与此类似的操作来填充表视图

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

流云如水 2024-11-12 21:26:12
NSMutableArray *ArrayList=[[NSMutableArray   alloc]initWithObjects: @"My First String", 
                                              @"My Second String",
                                              @"My Third String",
                                              @"My Forth String",
                                              @"My Fifth Sring",
                                              nil];
NSMutableArray *ArrayList=[[NSMutableArray   alloc]initWithObjects: @"My First String", 
                                              @"My Second String",
                                              @"My Third String",
                                              @"My Forth String",
                                              @"My Fifth Sring",
                                              nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文