如何返回两个自定义uitableviewcells

时间:2011-05-09 01:36:11

标签: iphone xcode uitableview

您好我只想弄清楚如何在我的uitableview上将两个不同的自定义uitableviewcells加载到两个不同的部分...只是不确定如何继续...这里是我目前的代码

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

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

    //Registration Button
    static NSString *CellIdentifier = @"CustomRegCell";
    static NSString *CellNib = @"LogInCustomCell";

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];
    }
    return cell;

}

/////// NEW ATTEMPT .... :(如果你可以称之为......

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

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

    if (indexPath.section == 0) 
    {
        //Registration Button
        static NSString *CellIdentifier = @"CustomRegCell";
        static NSString *CellNib = @"LogInCustomCell";

        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
            cell = (UITableViewCell *)[nib objectAtIndex:0];
        }
        return cell;        

    }
    else if (indexPath.section == 1)
    {
        //Registration Button
        static NSString *CellButtonIdentifier = @"CustomButtonCell";
        static NSString *CellButtonNib = @"LogInCustomCell";

        UITableViewCell *cellButton = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellButtonIdentifier];
        if (cellButton == nil) {
            NSArray *nibButton = [[NSBundle mainBundle] loadNibNamed:CellButtonNib owner:self options:nil];
            cellButton = (UITableViewCell *)[nibButton objectAtIndex:0];
        }
        return cellButton;      

    }
    return nil;

}

1 个答案:

答案 0 :(得分:3)

使用section变量的indexPath属性:

if (indexPath.section == 0) 
{
    // do this
}
else if (indexPath.section == 1)
{
    // do that
}

部分编号基于它们出现的顺序。您要在tableView中显示的第一部分将是0,依此类推。

相关问题