Objective-C:tableview

时间:2016-10-26 11:29:04

标签: ios objective-c xcode uitableview

我有UITableView,其中包含2个不同的自定义单元格。一个是 ExerciseTime ,一个是 RestTime 。我希望在每2个 ExerciseTime 单元格之间插入 RestTime 单元格:

My TableView

这是我的代码:

-Height for row

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // RestTime
    if (indexPath.row % 2 == 1) {
        return 40.0f;
    }

    // ExerciseTime
    else {
        return 65.0f;
    }
}

- 部分中的行数(我无法找到为 RestTime ExerciseTime 设置行数的方法)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return (self.preset.blocks.count * 2) - 1;
}

-Cell for row

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row % 2 == 1) {
        RestTimeTableViewCell *restTimeCell = (RestTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:RestTimeTableViewCellIdentifier  forIndexPath:indexPath];
        RestTime *restTime = (RestTime *)[self.restTimeArray objectAtIndex:indexPath.row];
        //CustomCell
        return restTimeCell;
    }else{
        ExerciseTimeTableViewCell *exerciseTimecell = (ExerciseTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ExerciseTimeTableViewCellIdentifier  forIndexPath:indexPath];
        ExerciseTime *exerciseTime = [self.exerciseTimeArray objectAtIndex:indexPath.row];
        //CustomCell
        return exerciseTimecell;
    }
    return nil;
}

如何创建一个包含2个自定义单元格的tableView?

2 个答案:

答案 0 :(得分:0)

如果你有n个exerciseTime,你将可以插入n-1个restTimes。所以

试试这个......

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.exerciseTime.count + (self.restTime.count >= self.exerciseTime.count) ? self.exerciseTime.count-1 : self.restTime.count;
}

现在,将exerciseTime添加到偶数位置,并将restTime添加到奇数位置,直到这两个列表中都存在当前索引。如果当前索引超过restTime,则继续添加exerciseTime。如果有效,请告诉我。

答案 1 :(得分:0)

第一次返回计数为jQuery中两个数组的总和,然后在numberOfRowsInSection中尝试这样的事情。

cellForRowAtIndexPath

输出:我通过将- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // RestTime if (indexPath.row % 3 == 0) { return 40.0f; } // ExerciseTime else { return 65.0f; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; if (indexPath.row % 3 == 0) { RestTimeTableViewCell *restTimeCell = (RestTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:RestTimeTableViewCellIdentifier forIndexPath:indexPath]; //Access you object from array like this NSInteger index = (NSInteger) (indexPath.row / 3); RestTime *restTime = (RestTime *)[self.restTimeArray objectAtIndex:index]; } else { ExerciseTimeTableViewCell *exerciseTimecell = (ExerciseTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ExerciseTimeTableViewCellIdentifier forIndexPath:indexPath]; //Access you object from array like this NSInteger index = (indexPath.row - (NSInteger) (indexPath.row / 3)); ExerciseTime *exerciseTime = [self.exerciseTimeArray objectAtIndex:(index - 1)]; } return cell; } 的背景颜色设置为 红色 来尝试此操作RestTimeTableViewCell的绿色

enter image description here