在我的应用中。我想当用户选择任何行时,它将显示带有textfield的警报,以便用户写入文本并显示detailtext标签。我从警报文本中获取了文本,但我无法获取索引选择哪一行以及何时显示文本。这是我的代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tblView deselectRowAtIndexPath:indexPath animated:YES];
if([arrProductData containsObject:[arrProduct objectAtIndex:indexPath.row]])
{
[arrProductData removeObject:[arrProduct objectAtIndex:indexPath.row]];
} else
{
[arrProductData addObject:[arrProduct objectAtIndex:indexPath.row]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add Quantity"
message:@"Please enter quantity"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:@"Cancel", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
[tableView reloadData];
}
警报措施方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
arrQuantity = [[NSMutableArray alloc]init];
[arrQuantity addObject:[alertView textFieldAtIndex:0].text];
NSLog(@"%@", [alertView textFieldAtIndex:0].text);
[self.tblView reloadData];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath] ;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if (tableView == self.tblView)
{
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[arrQuantity objectAtIndex:indexPath.row]];
}
if([arrProductData containsObject:[arrProduct objectAtIndex:indexPath.row]])
{
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
}
}
我也使用两个表view.so当我点击确定应用程序将崩溃我得到错误数组bounds.please帮助我
答案 0 :(得分:0)
您可以添加说明
[alert setTag:indexPath.row];
之前[alert show];
在if alertView
的{{1}} clickedButtonAtIndex
方法中,您可以打印索引,就像验证一样
if (buttonIndex == 0)
答案 1 :(得分:0)
将tag设置为alertView作为表的索引。
alert.tag = indexPath.row; //This will give you indexPath for clicked row.
答案 2 :(得分:0)
我明白了
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath] ;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if (tableView == self.tblView)
{
NSDictionary *dict = [arrProduct objectAtIndex:indexPath.row];
NSLog(@"");
cell.textLabel.text = [NSString stringWithFormat:@"%@",[dict valueForKey:@"product_name"]];
if (selectedIndex == indexPath.row)
{
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[arrQuantity objectAtIndex:selectedIndex]];
}
if([arrProductData containsObject:[arrProduct objectAtIndex:indexPath.row]])
{
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
}
}
答案 3 :(得分:0)
@property(nonatomic,strong)NSMutableDictionary * arrQuantity;
在viewDidLoad中:
arrQuantity = [[NSMutableDictionary alloc] init];
[alert setTag:indexPath.row];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSMutableDictionary *arrQuantity;
if (buttonIndex == 0) {
NSString *text = [alertView textFieldAtIndex:0].text;
if (text.length > 0) {
[arrQuantity setObject:text forKey:@(alertView.tag)];
[self.tblView reloadData];
}
}
}
修改cellForRowAtIndexPath:
if (tableView == self.tblView)
{
NSString *text = arrQuantity[@(indexPath.row)];
if (text.length) {
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",arrQuantity[@(indexPath.row)]];
}
}