【问题标题】:Scrolling tableview changes cell content滚动表格视图更改单元格内容
【发布时间】:2013-01-21 07:14:22
【问题描述】:
  1. 我在同一个视图 (ScrollView) 中一个接一个地分别加载两个 tableViews。但是当我尝试滚动第一个 tableView 时加载它们后,它的单元格(第一个和最后一个)内容更改为第二个 tableViews 单元格(第一个也是最后一个)内容。不明白我哪里出错了?

这是我正在处理的代码:

- (void)viewDidLoad
{
      self.tableView1=[[UITableView alloc] initWithFrame:CGRectMake(10,10,310,250) style:UITableViewStylePlain];
        self.tableView1.delegate=self;
        self.tableView1.dataSource=self;
        self.tableView1.rowHeight=80;
        self.tableView1.scrollEnabled = YES;
        currenttableView=@"1";
        [testscroll addSubview:self.tableView1];
 [self performSelector:@selector(callWebserviceMethod) withObject:nil afterDelay:0];
}
-(void)callWebserviceMethod
{
  self.tableView2=[[UITableView alloc] initWithFrame:CGRectMake(10,self.tableView1.frame.origin.y+self.tableView1.frame.size.height+6,310,230) style:UITableViewStylePlain];
        self.tableView2.delegate=self;
        self.tableView2.dataSource=self;
        self.tableView2.rowHeight=80;
         currenttableView=@"2";
        self.tableView2.scrollEnabled = YES;
        [testscroll addSubview:self.tableView2];
}


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


    static NSString *CellIdentifier=@"Cell";

    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault    reuseIdentifier:CellIdentifier]autorelease];

    }
    cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;


    if(currenttableView==@"1")
    {


        cell.textLabel.text = [[filtered objectAtIndex:indexPath.row] objectForKey:@"CompanyName"] ;   //filtered is NSMutableArray
        cell.textLabel.tag=3;
        cell.textLabel.font=[UIFont boldSystemFontOfSize:12];


        UIButton *showDirections = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        showDirections.frame = CGRectMake(10.0f, 52.0f, 85.0f, 20.0f);
        [showDirections setTitle:@"Direction" forState:UIControlStateNormal];
        [showDirections setTag:indexPath.row];
        [showDirections.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
        showDirections.backgroundColor=[UIColor lightTextColor];
        [ showDirections setBackgroundImage:[UIImage imageNamed:@"background-button.png"] forState:UIControlStateNormal];
        showDirections.titleLabel.textColor=[UIColor whiteColor];

        [cell addSubview:showDirections];
        [showDirections addTarget:self action:@selector(showDirectionsfordC:)forControlEvents:UIControlEventTouchUpInside];



        return cell;

    }


     if(currenttableView==@"2")
    {


        cell.textLabel.text = [[places objectAtIndex:indexPath.row] objectForKey:@"name"] ;    //places is NSMutableArray
        cell.textLabel.lineBreakMode=UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines=0;
        cell.textLabel.font=[UIFont boldSystemFontOfSize:12];
        cell.textLabel.tag=3;




        UIButton *showDirections = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        showDirections.frame = CGRectMake(10.0f, 52.0f, 85.0f, 20.0f);
        [showDirections setTitle:@"Direction" forState:UIControlStateNormal];
        [showDirections setTag:indexPath.row];
        [showDirections.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
        showDirections.backgroundColor=[UIColor lightTextColor];
        [ showDirections setBackgroundImage:[UIImage imageNamed:@"background-button.png"] forState:UIControlStateNormal];
        showDirections.titleLabel.textColor=[UIColor whiteColor];

        [cell addSubview:showDirections];
        [showDirections addTarget:self action:@selector(showDirections:)forControlEvents:UIControlEventTouchUpInside];

        NSString *vicinity1 = [[places objectAtIndex:indexPath.row]objectForKey:@"vicinity"];
        UILabel *vicinity = [[UILabel alloc]initWithFrame:CGRectMake(10, 45, 320, 10)];
        vicinity .font=[UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:12];
        [vicinity  setTextAlignment:UITextAlignmentLeft];
        vicinity.text=[NSString stringWithFormat:@"%@",vicinity1];

        vicinity.tag=4;
        [cell addSubview:vicinity ];
        vicinity.hidden=YES;
        [vicinity release];

        return cell;



    }

    }

我哪里出错了?

2.. 当 tableView2 为空时,ScrollView 仅加载 tableView1。然后当我尝试滚动 tableView1 时,我收到错误:Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array' 在tableView2的这一行

cell.textLabel.text = [[places objectAtIndex:indexPath.row] objectForKey:@"name"] ;//error

我怎样才能让它继续下去?

【问题讨论】:

    标签: iphone xcode uitableview


    【解决方案1】:

    与您的第一点一样,您需要将CellIdentifier 替换为

      NSString *CellIdentifier =[NSString stringWithFormat:@"%d",indexPath.row];
    

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    

    第二点错误说你已经从placesArray设置了cell.textLabel.text,在任何索引上都没有任何数据或键,如name,你的数组为Nil,这就是你的应用在滚动时崩溃的原因

    【讨论】:

    • 对于第一个问题,当我尝试使用您的代码时,问题仍然存在。即使单元格内容已更改。我还应该修改其他什么吗?
    • 把这个 if 条件 currenttableView=1 或其他 if 放入 if(cell == nil){}
    • 当我尝试滚动 tableView1 时跳转到 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 和 if(currenttableView==@"2").How我可以识别哪个 tableView 正在滚动吗?
    • if conditio if (tableView == yourTableOutlateName){}
    • Nitin 部分解决了,即单元格的内容文本没有改变。我明白了。从我的代码中可以看出,我在 tableView1 和 tableView2 中创建了 UIButtons。所以当我开始滚动tableView 的按钮文本颜色从白色变为黑色。我该如何解决?
    猜你喜欢
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2016-11-23
    • 1970-01-01
    相关资源
    最近更新 更多