UITableview部分错误 - objc_msgSend

时间:2009-07-06 09:15:42

标签: iphone uitableview

如果我制作了一个部分,则会发生错误。

我已经尝试将部分索引设置为0和1,但这也没有帮助。

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

    static NSString *CellIdentifier = @"Cell";
    static NSString *CellIdentifier1 = @"Cell1";


    if(indexPath.section == 1) {
        if(indexPath.row == 0) {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.text = @"test 1";

        }

        return cell;
    }
    else if(indexPath.row == 1) {
        UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell1 == nil) {
            cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
            cell1.text = @"test 2";

        }

        return cell1;
    }
    }

else if(indexPath.section == 2) {
    if(indexPath.row == 0) {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            cell.text = @"test 1";

        }

        return cell;
    }
    else if(indexPath.row == 1) {
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell1 == nil) {
            cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
            cell1.text = @"test 2";

        }

        return cell1;
    }
}
}

3 个答案:

答案 0 :(得分:0)

节和行从0开始。据我所知,你没有为第0节第0行的indexPath返回一个单元格。

编辑:重新发布源代码(更易读):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    static NSString *CellIdentifier1 = @"Cell1";
    if(indexPath.section == 1) {
        if(indexPath.row == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
                cell.text = @"test 1";
            }
            return cell;
        }
        else if(indexPath.row == 1) {
            UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
            if (cell1 == nil) {
                cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
                cell1.text = @"test 2";
            }
            return cell1;
        }
    }
    else if(indexPath.section == 2) {
        if(indexPath.row == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
                cell.text = @"test 1";
            }
            return cell;
        }
        else if(indexPath.row == 1) {
            UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
            if (cell1 == nil) {
                cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
                cell1.text = @"test 2";
            }
            return cell1;
        }
    }
}

答案 1 :(得分:0)

基本问题。你有没有更新部分的数量 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)部分。 正常的obj msg发送错误..来自你已经发布的东西或你不拥有的东西.. 例如... 在表视图的init方法中,您编写此代码 sectionArray = [NSArray arraywithcontentsoffile:xyz.plist]

并且在numberofRowsInSection中你使用这样的东西 [sectionarray count];

其中sectionArray是一个实例变量..

答案 2 :(得分:-1)

感谢您的回复!

我再次编写了整个代码,并在每次添加一行代码时运行它。这有帮助。 我认为问题是if(indexPath.section == 0)等等。 我只为3个部分中的2个设置内容。

但我不确定这是不是问题。

感谢您的大力帮助!

作为初学者目标c并不那么容易:)