[英]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.