简体   繁体   English

如何在动态表格的每一行添加一个按钮?

[英]How to add a button in each row of a dynamic table?

I am re-using an Objective-C code where there is a dynamic tableView that display bluetooth devices in the area.我正在重新使用 Objective-C 代码,其中有一个动态 tableView 显示该区域中的蓝牙设备。 I would like to add a button on each row of this tableView that would trigger an action and display a content at the bottom of the screen.我想在这个 tableView 的每一行上添加一个按钮,它会触发一个动作并在屏幕底部显示一个内容。

The current and interesting part of code is代码的当前和有趣的部分是

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    CLBeacon *beacon = (CLBeacon*)[self.beacons objectAtIndex:indexPath.row];
    NSString *proximityLabel = @"";
    switch (beacon.proximity) {
        case CLProximityFar:
            proximityLabel = @"Far";
            break;
        case CLProximityNear:
            proximityLabel = @"Near";
            break;
        case CLProximityImmediate:
            proximityLabel = @"Immediate";
            break;
        case CLProximityUnknown:
            proximityLabel = @"Unknown";
            break;
    }

    cell.textLabel.text = proximityLabel;

    NSString *detailLabel = [NSString stringWithFormat:@"Major: %d, Minor: %d, RSSI: %d, UUID: %@",
                             beacon.major.intValue, beacon.minor.intValue, (int)beacon.rssi, beacon.proximityUUID.UUIDString];
    cell.detailTextLabel.text = detailLabel;

    return cell;
}

I am wondering what would be the best way to do it, and where to insert the button in this code.我想知道什么是最好的方法,以及在此代码中插入按钮的位置。 I am not familiar with Objective-C dev and looking for all kind of example/illustration for this problem.我不熟悉 Objective-C 开发人员,正在寻找针对此问题的各种示例/插图。

You can add your button in your cell's init part, that is您可以在单元格的 init 部分添加按钮,即

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    // create your button and add it to your cell
    UIButton *button = ....
    [cell.contentView addSubview:button];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 header 添加到表视图中的每一行 - How to add header to each row in a table view 如何在UITableview iOS的每一行中添加多个按钮和标签 - How to add multiple button and label in each row on UITableview iOS 如何在行单击事件的每一行下方的表中以下拉样式添加子视图? - How to add a subview in drop-down style in a table below each row on row-click event? 如何处理UITableView每行中每个按钮的按钮单击 - how to handle button click for each button in each row of UITableView WKInterfaceTable如何识别每一行中的按钮? - WKInterfaceTable how to identify button in each of the row? UITableViewCell中的iOS动态按钮标记未检测到表中的正确行 - iOS Dynamic Button tagging in UITableViewCell does not detect correct row in table 如何显示循环到表行的每个元素? - How to display each elements of loop to table row? 如何为每行创建一个具有自动高度的表格视图 - how to create a table view with autoheight for each row 如何在一个视图控制器上使用按钮将文本添加到另一个视图控制器的表视图中的行? - How can I use a button on one view controller to add text to a row in a table view in another view controller? 如何在UINavigation栏中添加动态/多按钮? - How to add dynamic/multi button in UINavigation Bar?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM