【问题标题】:Receive Data from UITableViewCell从 UITableViewCell 接收数据
【发布时间】:2026-02-16 01:15:01
【问题描述】:

我有以下代码集:

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

    static NSString *CellIdentifier = @"customCell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:@"CustomCellView" owner:nil options:nil];
        //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        for(id currentObj in nibObjs)
        {
            if([currentObj isKindOfClass:[CustomCell class]])
            {
                cell = (CustomCell *)currentObj;
            }

        }
    }

    AssessmentDetail * anAssess = [module.assessmentDetails4 objectAtIndex:indexPath.row];
    [[cell labelAssessment] setText:anAssess.assessmentName4];
    return cell;
}

在我的自定义单元格中有一个UISlider。我想做的是使用一个按钮从每一行中检索每个 UISlider 的值,这样我就可以获得一个总值。

我想过这样做,但我不确定从那里去哪里:

- (IBAction) calculateTotal:(id)sender {

    static NSString *CellIdentifier = @"customCell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

}

感谢您的帮助。

【问题讨论】:

    标签: objective-c xcode uitableview custom-cell


    【解决方案1】:

    循环浏览 UITableView 的子视图:

    - (IBAction) calculateTotal:(id)sender {
        NSArray *subViews = [myTableView subviews];
        float total;
    
        for (int i = 0; i < subViews.count; i++)
        {
             id obj = [subViews objectAtIndex:i];
             if ([obj isKindOfClass:[CustomCell class]])
             {
                  total += [[obj getMySlider] getValue];
             }
        }
    
        // do something with total
    }
    

    【讨论】:

    • 需要注意的一点:如果您的单元格真正可出队,那么如果您有超过一整页的单元格,这只会返回屏幕上单元格的值。
    • 谢谢理查德,我在尝试代码时收集到了这一点。再次感谢