カスタムセルのボタンで警告を表示したい。 Objective Cでこれを行うにはどうすればよいですか? おかげUITableViewCellクラスからの警告を表示 - Objective c
2
A
答えて
0
は、カスタムテーブルビューのセルにコンテナView Controller
のweak
インスタンスを渡します。
セルにはが渡され、View Controller
となり、UIAlertController
と表示されます。
のように、これはなります達成するためにいくつかのサンプルコード:あなたはちょうどあなたがそれに表示するものは何でもラベルとボタンと1つのカスタムセルを作成する必要が
// CustomTableViewCell.h
@interface CustomTableViewCell : UITableViewCell
@property (nonatomic, weak) __kindof UIViewController *controllerDelegate; // __kindof used to avoid importing the View Controller class, we only need a controller object & nothing else, hence using __kindof will suffice
@end
// CustomTableViewCell.m
@implementation CustomTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
...
}
return self;
}
- (void)setControllerDelegate:(__kindof UIViewController *)controllerDelegate{
_controllerDelegate = controllerDelegate;
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert" message:@"This is an alert!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:cancelButton];
UIAlertAction *proceedButton = [UIAlertAction actionWithTitle:@"Proceed" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
... // add custom actions on alert button tap here
}];
[alertController addAction:proceedButton];
[_controllerDelegate presentViewController:alertController animated:YES completion:nil];
}
@end
// MyViewController.m
@interface MyViewController()
...
@end
@implementation MyViewController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:taskTableIdentifier forIndexPath:indexPath];
cell.controllerDelegate = self;
return cell;
}
@end
1
。
CustomTableViewCell.h
#import <UIKit/UIKit.h>
@interface CustomTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *lblName;
@property (weak, nonatomic) IBOutlet UIButton *btnAlert;
@end
CustomTableViewCell.h
#import "CustomTableViewCell.h"
@implementation CustomTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
今、あなたのコントローラでは、あなたは、テーブルビューを作成して、カスタムセルを与え、ボタン方式をバインドする必要がありますin cellForRowAtIndexPath。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static N
SString *simpleTableIdentifier = @"CustomTableViewCell";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = (CustomTableViewCell*)[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.lblName.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
cell.btnAlert.tag = indexPath.row;
[cell.btnAlert addTarget:self action:@selector(alertButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(IBAction)alertButtonClicked:(id)sender
{
[[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Button Clicked" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil] show];
}
+0
@ user2254968解決策を手に入れましたか? – Nirmalsinh
関連する問題
- 1. モデルからの警告を表示
- 2. UIViewControllerを表示する際の警告
- 3. XcodeのObjective-Cの警告は
- 4. Objective-C SingletonsとLLVM/clangのリーク警告
- 5. 警告ダイアログボックスに警告メッセージが表示されませんか?
- 6. 問題(警告モジュールからの)警告
- 7. iOS - スレッドと警告表示
- 8. 任意のアクティビティでバックグラウンドプロセスからの警告ダイアログを表示
- 9. 甘い警告。警告ボックスにカウントダウンを表示
- 10. サードパーティのフレームワーク/ライブラリからの警告が表示されない
- 11. このC++コードは奇妙な警告を表示します
- 12. Visual Studio 2012でMSBuild警告を表示
- 13. 警告と時計のアラームを表示
- 14. samsungスマートTVの警告を表示
- 15. ダイアログを表示入力テキストの警告
- 16. 別のウィンドウで警告メッセージを表示
- 17. broatcastReceiverの警告ダイアログを表示する
- 18. iPhoneの警告表示ビューコントローラを除外
- 19. Objective-cのアセンブリコードを表示
- 20. AndroidのSMSの警告がポップアップ表示
- 21. Objective-C警告RunUnitTestsは廃止されました。 Xcodeの7
- 22. を表示する警告メッセージ
- 23. Qt4で標準警告アイコンを表示
- 24. 警告ダイアログにメッセージを表示
- 25. WPF:素敵な警告を表示
- 26. クリックイベントで警告メッセージボックスを表示
- 27. 再度警告を表示しない
- 28. SwiftからObjective-CへのスーパービューでUITableViewCellを取得
- 29. 表示し、キー値の配列警告
- 30. iPadアプリのネットワーク接続警告表示
Googleでこれを試しましたか? "objective-c button tableview"は、役に立つかもしれない多くの結果を提供します。 – Ralfonso
私はボタンを追加する方法を知っていますが、私は警告を表示することはできません – user2254968
ああ。さて、これは、StackOverflowを使用してより良い応答を得ること、できるだけ具体的な問題として質問したり、コードの一部を投稿したりする手助けをする機会です。マントラは、あなたがあなたの質問を洗練しようとするときに、あなた自身で解決策を見つけるのが普通です。あなたは、ボタンプレスからの警告を表示するために特に検索しましたか? UIButtonからのUIAlertのグーグルリングはおそらく解決策を提供します。 – Ralfonso