我有一个应用程序从SQLite数据库中提取信息并填充TableView中的单元格。但是,我希望用户能够单击一个单元格,然后能够通过AlertView编辑该单元格中的内容。以下是我的代码的一部分:
//Set up cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = [resultSet objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Question" message:@"Please enter a new question" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
txtNewQuestion = [av textFieldAtIndex:0];
txtNewQuestion.clearButtonMode = UITextFieldViewModeWhileEditing;
av.delegate = self;
[txtNewQuestion setPlaceholder:@"new Question"];
[av show];
[self.txtNewQuestion becomeFirstResponder];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
CustomCell *cell;
enteredQuestion = txtNewQuestion.text;
cell.customLabel.text = enteredQuestion;
NSLog(@"%@", enteredQuestion);
NSLog(@"%@", cell.customLabel.text);
}
[self.tableView reloadData];
}
非常感谢任何帮助。感谢。
答案 0 :(得分:1)
我会改用didSelectRowAtIndexPath,我有时会喜欢使用标签:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Question" message:@"Please enter a new question" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
av.delegate = self;
av.tag = [indexPath row];
[av show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
[resultSet replaceObjectAtIndex:alertView.tag withObject:[alertView textFieldAtIndex:0].text];
[table reloadData];
}
}
答案 1 :(得分:0)
如何在UIAlertView委托中获取单元格?
你如何保存enteredQuestion
?
当您致电reloadData
时,整个表格将被重建(Documentation)