【问题标题】:How can i name cells navigation controller?如何命名单元格导航控制器?
【发布时间】:2011-07-20 13:14:08
【问题描述】:
 if (indexPath.row % 2 == 0) {
    // EVEN
    cell = [tableView dequeueReusableCellWithIdentifier:@"EvenCell"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"EvenCell"] autorelease];
        UIView *bg = [[UIView alloc] initWithFrame:cell.frame];

        UIColor *colour = [[UIColor alloc] initWithRed: (208.0/255.f) green: (231.0/255.f) 
                                                  blue: (241.0/255.f) alpha: 1.0];
        bg.backgroundColor = colour; 
        cell.backgroundView = bg;
        cell.textLabel.backgroundColor = bg.backgroundColor;
        [bg release];
        cell.textLabel.text = [items objectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

} else {
    // ODD

    cell = [tableView dequeueReusableCellWithIdentifier:@"OddCell"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"OddCell"] autorelease];
        UIView *bg = [[UIView alloc] initWithFrame:cell.frame];

        UIColor *colour = [[UIColor alloc] initWithRed: (143.0/255.f) green: (169.0/255.f) 
                                                  blue: (180.0/255.f) alpha: 1.0];
        bg.backgroundColor = colour;
        cell.backgroundView = bg;
        cell.textLabel.backgroundColor = bg.backgroundColor;
        [bg release];
        cell.textLabel.text = [items objectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }
} 

return cell;
}

这是我的自定义单元格。第一个是偶数,第二个是奇数。我的数组有 194 个元素,但是当我运行应用程序时,我只能在第 10 个元素之后看到 10 个元素,它再次转到第一个元素给我相同的 10 个元素。谁能告诉我这里出了什么问题?

【问题讨论】:

  • 这里没有错...numberOfRows 返回什么?错误一定存在
  • 我检查了项目数组 NSLog(@"%d",[items count]);它给了我 194 我从 url 读取它们并给了我真实的数字以及我如何查看行数
  • 是这个方法返回的数字吗?
  • 不,我在 - (void)viewDidLoad { [super viewDidLoad]; NSURL *URL=[NSURL URLWithString:@"trevesstudios.com/osman"]; NSString *content=[NSString stringWithContentsOfURL:URL 编码:NSUTF8StringEncoding 错误:nil]; NSString *list=[content stringByReplacingOccurrencesOfString:@"
    "withString:@"" ]; NSArray *listItems = [list componentsSeparatedByString:@"\n"]; array=[listItems retain]; NSLog(@"%d",[items count]);

标签: iphone objective-c uitableview uinavigationcontroller


【解决方案1】:

试试这个。

if (indexPath.row % 2 == 0) {
// EVEN
        cell = [tableView dequeueReusableCellWithIdentifier:@"EvenCell"];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"EvenCell"] autorelease];
            UIView *bg = [[UIView alloc] initWithFrame:cell.frame];


        UIColor *colour = [[UIColor alloc] initWithRed: (208.0/255.f) green: (231.0/255.f) 
                                                  blue: (241.0/255.f) alpha: 1.0];
        bg.backgroundColor = colour; 
        cell.backgroundView = bg;
        cell.textLabel.backgroundColor = bg.backgroundColor;
        [bg release];
    }
        cell.textLabel.text = [items objectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


} else {
    // ODD

    cell = [tableView dequeueReusableCellWithIdentifier:@"OddCell"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"OddCell"] autorelease];
        UIView *bg = [[UIView alloc] initWithFrame:cell.frame];

        UIColor *colour = [[UIColor alloc] initWithRed: (143.0/255.f) green: (169.0/255.f) 
                                                  blue: (180.0/255.f) alpha: 1.0];
        bg.backgroundColor = colour;
        cell.backgroundView = bg;
        cell.textLabel.backgroundColor = bg.backgroundColor;
        [bg release];
    }
        cell.textLabel.text = [items objectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


  } 

  return cell;
 }

【讨论】:

    【解决方案2】:

    为了让您的视图显示正确的行数,请确保 -(NSUInteger)tableView:numberOfRowsInSection:(NSUInteger)section 返回正确的数字。 (在你的情况下,你应该返回[items count])。

    【讨论】:

    • 感谢 vince 的耐心和帮助 lanc 解决方案是真的。
    • 我把 cell.textLabel.text = [items objectAtIndex:indexPath.row]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;将代码写入 if 语句。他取出了那部分。
    猜你喜欢
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 2019-05-23
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多