TableView表现得很奇怪

时间:2013-12-04 18:16:20

标签: ios objective-c uitableview

我的tableView(phpush)表现得异乎寻常。 0部分的单元格随机出现在不同的行中,有时会从表格的顶部消失。以下是我认为可能导致问题的一些方法:

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (tableView==_phpush) {

        if (indexPath.section==0) {

            UIButton *pushButtonDate;
            UIButton *pushButtonPush;
            UIButton *edit;

            if (cell == nil) {

                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
                pushButtonPush = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                [pushButtonPush addTarget:self action:@selector(pushButtonPushPressed:)forControlEvents UIControlEventTouchUpInside];
                [pushButtonPush setTitle:@"Push-Ups" forState:UIControlStateNormal];
                pushButtonPush.frame = CGRectMake(150,7,97,30);
                pushButtonDate.tag = 3;
                [cell.contentView addSubview:pushButtonPush];

                pushButtonDate = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                [pushButtonDate addTarget:self action:@selector(pushButtonDatePressed:)forControlEvents: UIControlEventTouchUpInside];
                [pushButtonDate setTitle:@"Date" forState:UIControlStateNormal];
                pushButtonDate.frame = CGRectMake(11,7,97,30);
                pushButtonDate.tag = 4;
                [cell.contentView addSubview:pushButtonDate];

                edit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                [edit addTarget:self action:@selector(editButtonPressed:)forControlEvents:UIControlEventTouchUpInside];
                [edit setTitle:@"Edit" forState:UIControlStateNormal];
                edit.frame = CGRectMake(200,7,97,30);
                edit.tag = 5;
                [cell.contentView addSubview:edit];
            }
            else {
                pushButtonPush = [cell.contentView viewWithTag:(3)];
                pushButtonDate = [cell.contentView viewWithTag:(4)];
                edit = [cell.contentView viewWithTag:5];
            }
            return cell;
        }
        else {
            UILabel *pushLabel;
            UILabel *pushLabel2;
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
                pushLabel = [[UILabel alloc]initWithFrame:CGRectMake(150,7,97,30)];
                pushLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(10,7,150,30)];
                NSArray *array;
                if (sortByDate==true) {
                    array = [[pushDictionary allKeys]sortedArrayUsingSelector
                             :@selector(localizedCaseInsensitiveCompare:)];
                }
                else {
                    array = [pushDictionary keysSortedByValueUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
                }
                pushLabel.text = [pushDictionary valueForKey:[array objectAtIndex:indexPath.row]];
                NSString *pushLabel2String = [[array objectAtIndex:indexPath.row] substringToIndex:MIN(10, [[array objectAtIndex:indexPath.row] length])];
                NSArray *pushLabel2Array = [pushLabel2String componentsSeparatedByString:@"-"];
                pushLabel2.text = [NSString stringWithFormat:@"%@/%@/%@",[pushLabel2Array objectAtIndex:1],[pushLabel2Array objectAtIndex:2],[pushLabel2Array objectAtIndex:0]];
                pushLabel.tag = 1;
                pushLabel2.tag = 2;
                [cell.contentView addSubview:pushLabel];
                [cell.contentView addSubview:pushLabel2];
            }
            else {
                pushLabel = [cell.contentView viewWithTag:1];
                pushLabel2 = [cell.contentView viewWithTag:2];
            }
            return cell;
            }
        }
    else {
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        else {

        }
        return cell;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView==_phpush) {
        return 2;
    }
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView==_phpush) {
        if (section==0) {
            return 1;
        }
        return  [pushDictionary allKeys].count;
    }
    return 1;
}

我想知道我是否有任何重大错误会导致这个问题。我对iOS编程很新,所以如果我犯了一个明显的错误,请原谅我。我是否需要发布更多代码?

谢谢,

约旦

2 个答案:

答案 0 :(得分:1)

你的tableViewCell出现了问题。

else声明中

else {
            pushButtonPush = [cell.contentView viewWithTag:(3)];
            pushButtonDate = [cell.contentView viewWithTag:(4)];
            edit = [cell.contentView viewWithTag:5];
     }

你永远不会修改这些按钮的内容。您正在设置的变量未使用,因为您不更改按钮文本,也不删除或添加视图。如果他们需要做一些不同的事情,你需要在这里指定它。

答案 1 :(得分:0)

单元格在滚动时被重复使用,当一个单元格被重用时,它的所有元素仍然存在于其中。 因此,如果您在单元格中设置了一些文本,当它被重用时,文本仍然存在,因此您需要将其设置为nil或者如果您不想要它将其删除。 你不应该在cellForRowAtIndexPath中分配任何东西,就像你滚动你在旧的上面添加越来越多的对象一样。

看一下UITableViewCell的子类化。