如何分离输入字符串并在 table 视图中填充它重复了多少次并将索引传递给下一个
how to separate input string and populate on table view with how many times it repeated and passe the index to next
我是 iOS 和 ObjectiveC 的新手。我到处搜索,但没有找到所需的选项,而且我无法理解。
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @""
message: @"Enter input text"
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Text";
textField.textColor = [UIColor blueColor];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.borderStyle = UITextBorderStyleRoundedRect;
}];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
{
NSArray * textfields = alertController.textFields;
UITextField * namefield = textfields[0];
inputstring=namefield.text;
for (int i = 0; i < [inputstring length]; i++) {
[array addObject:[NSString stringWithFormat:@"%c", [inputstring characterAtIndex:i]]];
}
_firsttblview.delegate=self;
_firsttblview.dataSource=self;
[_firsttblview reloadData];
}]];
[self presentViewController:alertController animated:YES completion:nil];
为了在 table 视图中显示,我使用了以下代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
firstTableViewCell *cell = [_firsttblview dequeueReusableCellWithIdentifier:@"firstTableViewCell"];
if (cell == nil)
{
cell = [[[NSBundle mainBundle]loadNibNamed:@"firstTableViewCell" owner:self options:nil]objectAtIndex:0];
}
int count=0;
cell.charlbl.text=[array objectAtIndex:indexPath.row];
NSString *singchstr=array[indexPath.row];
for (int a=0; a<array.count; a++) {
NSString *chrstrh=array[a];
if ([chrstrh isEqualToString:singchstr]) {
count ++;
}
}
for (int b=0; b<indexPath.row; b++) {
NSString *charststr=array[b];
if ([singchstr isEqualToString:charststr]) {
count=0;
break;
}
}
cell.countlbl.text=[NSString stringWithFormat:@"%d",count];
return cell;
}
如果我单击索引路径,我必须使用该索引路径发送所有重复索引。
在下一个 table 我必须删除一个相似的字母,我将返回到那个索引路径中的字母应该删除。
在选择行中:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
firstTableViewCell *cell = [_firsttblview cellForRowAtIndexPath:indexPath];
NSMutableArray *indexpatharray=[[NSMutableArray alloc]init];
for (int i=0; i<array.count; i++) {
NSString *charsring=array[i];
if ([cell.charlbl.text isEqualToString:charsring]) {
[indexpatharray addObject:[NSNumber numberWithInt:i]];
}
}
secondViewController *viewcontroller = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"secondViewController"];
viewcontroller.charStr = cell.charlbl.text;
viewcontroller.charArr = indexpatharray;
viewcontroller.delegate = self;
[self.navigationController pushViewController:viewcontroller animated:YES];
}
-(void)passthearray:(NSMutableIndexSet *)set //meth
{
[array removeObjectsAtIndexes:set];
[_firsttblview reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _charArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
firstTableViewCell *cell = [_secondtblview dequeueReusableCellWithIdentifier:@"firstTableViewCell"];
if (cell == nil)
{
cell = [[[NSBundle mainBundle]loadNibNamed:@"firstTableViewCell" owner:self options:nil]objectAtIndex:0];
}
cell.charlbl.text = _charStr;
cell.countlbl.text = [NSString stringWithFormat:@"%@", _charArr[indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
firstTableViewCell *cell = [_secondtblview cellForRowAtIndexPath:indexPath];
[_charArr removeObjectAtIndex:indexPath.row];
[indexes addIndex:[cell.countlbl.text integerValue]];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
- (IBAction)submitbuttn:(id)sender {
[_delegate passthearray:indexes];
[self.navigationController popToRootViewControllerAnimated:YES];
}
@protocol reloadtabeldata <NSObject>
-(void)passthearray:(NSMutableIndexSet *)set;
@property(nonatomic,weak) id<reloadtabeldata> delegate;
我是 iOS 和 ObjectiveC 的新手。我到处搜索,但没有找到所需的选项,而且我无法理解。
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @""
message: @"Enter input text"
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Text";
textField.textColor = [UIColor blueColor];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.borderStyle = UITextBorderStyleRoundedRect;
}];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
{
NSArray * textfields = alertController.textFields;
UITextField * namefield = textfields[0];
inputstring=namefield.text;
for (int i = 0; i < [inputstring length]; i++) {
[array addObject:[NSString stringWithFormat:@"%c", [inputstring characterAtIndex:i]]];
}
_firsttblview.delegate=self;
_firsttblview.dataSource=self;
[_firsttblview reloadData];
}]];
[self presentViewController:alertController animated:YES completion:nil];
为了在 table 视图中显示,我使用了以下代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
firstTableViewCell *cell = [_firsttblview dequeueReusableCellWithIdentifier:@"firstTableViewCell"];
if (cell == nil)
{
cell = [[[NSBundle mainBundle]loadNibNamed:@"firstTableViewCell" owner:self options:nil]objectAtIndex:0];
}
int count=0;
cell.charlbl.text=[array objectAtIndex:indexPath.row];
NSString *singchstr=array[indexPath.row];
for (int a=0; a<array.count; a++) {
NSString *chrstrh=array[a];
if ([chrstrh isEqualToString:singchstr]) {
count ++;
}
}
for (int b=0; b<indexPath.row; b++) {
NSString *charststr=array[b];
if ([singchstr isEqualToString:charststr]) {
count=0;
break;
}
}
cell.countlbl.text=[NSString stringWithFormat:@"%d",count];
return cell;
}
如果我单击索引路径,我必须使用该索引路径发送所有重复索引。
在下一个 table 我必须删除一个相似的字母,我将返回到那个索引路径中的字母应该删除。
在选择行中:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
firstTableViewCell *cell = [_firsttblview cellForRowAtIndexPath:indexPath];
NSMutableArray *indexpatharray=[[NSMutableArray alloc]init];
for (int i=0; i<array.count; i++) {
NSString *charsring=array[i];
if ([cell.charlbl.text isEqualToString:charsring]) {
[indexpatharray addObject:[NSNumber numberWithInt:i]];
}
}
secondViewController *viewcontroller = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"secondViewController"];
viewcontroller.charStr = cell.charlbl.text;
viewcontroller.charArr = indexpatharray;
viewcontroller.delegate = self;
[self.navigationController pushViewController:viewcontroller animated:YES];
}
-(void)passthearray:(NSMutableIndexSet *)set //meth
{
[array removeObjectsAtIndexes:set];
[_firsttblview reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _charArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
firstTableViewCell *cell = [_secondtblview dequeueReusableCellWithIdentifier:@"firstTableViewCell"];
if (cell == nil)
{
cell = [[[NSBundle mainBundle]loadNibNamed:@"firstTableViewCell" owner:self options:nil]objectAtIndex:0];
}
cell.charlbl.text = _charStr;
cell.countlbl.text = [NSString stringWithFormat:@"%@", _charArr[indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
firstTableViewCell *cell = [_secondtblview cellForRowAtIndexPath:indexPath];
[_charArr removeObjectAtIndex:indexPath.row];
[indexes addIndex:[cell.countlbl.text integerValue]];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
- (IBAction)submitbuttn:(id)sender {
[_delegate passthearray:indexes];
[self.navigationController popToRootViewControllerAnimated:YES];
}
@protocol reloadtabeldata <NSObject>
-(void)passthearray:(NSMutableIndexSet *)set;
@property(nonatomic,weak) id<reloadtabeldata> delegate;