0

我有一个从 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];

}

任何帮助将不胜感激。谢谢。

4

2 回答 2

2

我会改用 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];
    }
}
于 2014-06-10T14:12:05.433 回答
0

你如何获得 UIAlertView 委托中的单元格?你如何保存enteredQuestion

当你调用reloadData整个表将被重建(文档)

于 2014-06-10T14:11:39.397 回答