I have created a custom UITableViewCell for a UITableView by dragging and dropping the labels and imageView from my Prototype Cell to a Custom Cell which I call QuizInfoCell.
That works fine for the particular TableView that I created the Prototype Cell in, but I can't get QuizInfoCell to work for any other TableView. How can I achieve that?
This is the code that I'm using in the TableView that created the Prototype Cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
QuizInfoCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"QuizInfoCell" forIndexPath:indexPath];
if (cell == nil){
cell = [[QuizInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"QuizInfoCell"];
}
cell.name.text = [title objectAtIndex:indexPath.row];
cell.author.text = [author objectAtIndex:indexPath.row];
cell.time.text = [[ValueHandler alloc] getDateString:[timeStamp objectAtIndex:indexPath.row]];
cell.plays.text = [#"Plays: " stringByAppendingString:[NSString stringWithFormat:#"%#", [plays objectAtIndex:indexPath.row ]]];
cell.ratingUps.text = [NSString stringWithFormat:#"%#", [ratingUps objectAtIndex:indexPath.row]];
cell.ratingDowns.text = [NSString stringWithFormat:#"%#", [ratingDowns objectAtIndex:indexPath.row]];
cell.qid.text = [NSString stringWithFormat:#"%#", [qid objectAtIndex:indexPath.row]];
return cell;
}
I suggest creating your QuizInfoCell as a new XIB file.
Press CMD + N
Select the User Interface submenu.
Create a new View and call it QuizInfoCell
In your new XIB delete the existing View element and instead drag and drop a new Table View Cell.
Recreate your custom UITableViewCell in the same way as you did before by dragging and dropping the labels and imageView
Finally give the new Cell an Identifier called QuizInfoCell, like this:
You can then load your new Custom UITableViewCell with Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
QuizInfoCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"QuizInfoCell" forIndexPath:indexPath];
if (cell == nil){
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:#"QuizInfoCell" owner:self options:nil];
cell = [nibContents objectAtIndex:0];
}
cell.name.text = [title objectAtIndex:indexPath.row];
cell.author.text = [author objectAtIndex:indexPath.row];
cell.time.text = [[ValueHandler alloc] getDateString:[timeStamp objectAtIndex:indexPath.row]];
cell.plays.text = [#"Plays: " stringByAppendingString:[NSString stringWithFormat:#"%#", [plays objectAtIndex:indexPath.row ]]];
cell.ratingUps.text = [NSString stringWithFormat:#"%#", [ratingUps objectAtIndex:indexPath.row]];
cell.ratingDowns.text = [NSString stringWithFormat:#"%#", [ratingDowns objectAtIndex:indexPath.row]];
cell.qid.text = [NSString stringWithFormat:#"%#", [qid objectAtIndex:indexPath.row]];
return cell;
}
Related
i here attached a snapshot of my sample output.now it has two cells that is according to arraycount which i used at number of rows in section method.i have calutalted the break time ,now i want to insert one more cells in between that two cells to dispay the break time . here is my code whic i did ....please help me to do this .thanks in advance
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"TableViewCell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSLog(#"%#",self.arrData1);
NSDictionary *tempDictionary= [self.arrData1 objectAtIndex:indexPath.row];
//NSLog(#"%#",tempDictionary);
NSString *strFunctionCodeValue =[tempDictionary objectForKey:#"function_code"];
NSString *strStartTimeValue = [tempDictionary objectForKey:#"start_time"];
NSString *strEndTimeValue = [tempDictionary objectForKey:#"end_time"];
NSString *strTotalUnitsValue =[tempDictionary objectForKey:#"total_units"];
NSString *strTotalTimeValue = [tempDictionary objectForKey:#"total_time"];
//NSString *strBreakTimeValue = [tempDictionary objectForKey:#"break_time"];
cell.lblFunctionCodeValue.text = strFunctionCodeValue;
cell.lblStartTimeValue.text = strStartTimeValue;
cell.lblEndTimeValue.text = strEndTimeValue;
cell.lblTotalUnitsValue.text =strTotalUnitsValue;
cell.lblTotalTimeValue.text = strTotalTimeValue;
//cell.lblBreakTimeValue.text = strBreakTimeValue;
return cell;
}
I have an array of names that I'm showing via tableview. You can select up to a total of 3 names, and you cannot re-select the same names. To do this I implemented the following code in cellForRowAtIndexPath:. When I run the code the names come up fine, but there are multiple red cells with names that I did not select.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *sectionTitle = [nameSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionNames = [names objectForKey:sectionTitle];
NSString *name = [sectionNames objectAtIndex:indexPath.row];
cell.textLabel.text = name;
if ([name isEqualToString: self.name1] || [name isEqualToString: self.name2] || [name isEqualToString: self.name3]) {
[cell setUserInteractionEnabled:NO];
cell.backgroundColor = [UIColor redColor];
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
Reading a similar problem here, they were saying that it's because the cells are being reused - but if this were true, how is the tableview still displaying the correct names in the correct position?
I tried to simplify the code into this, and still to no avail, there were multiple red cells.
myIP = [NSIndexPath indexPathForRow:0 inSection:0];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"TableCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *sectionTitle = [nameSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionNames = [names objectForKey:sectionTitle];
NSString *name = [sectionNames objectAtIndex:indexPath.row];
cell.textLabel.text = name;
if (indexPath == myIP) {
cell.backgroundColor = [UIColor redColor];
}
return cell;
}
I can post a screenshot if needed. Note: The intended names were correctly labeled with red.
The issue is happening due to cell re-using. When a cell with red background is re-used it'll still be in red background, you are not re-setting it anywhere in your code. You need to put a else case to your if condition used in cellForRowAtIndexPath: method.
if ([name isEqualToString: self.name1] || [name isEqualToString: self.name2] || [name isEqualToString: self.name3])
{
[cell setUserInteractionEnabled:NO];
cell.backgroundColor = [UIColor redColor];
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
[cell setUserInteractionEnabled:YES];
cell.backgroundColor = [UIColor clearColor];
// Other stuffs
}
I have a problem using a UITableView inside a UIViewController:
When I start the app the UITableView is shown, but the data from _opponents isn't shown.
When I start scrolling the first cell outside the UITableView, now this cell gets updated.
Also, when I trigger [_topponents reloaddata] manually, the whole UITableView shows the correct data.
What do I have to add or change, so that the data is shown from the beginning?
dataSource and delegate are both connected with the class in the storyboard
Here is my code:
viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
myappdel = (AppDelegate *)[UIApplication sharedApplication].delegate;
self.title = #"New Match";
_lopponent.text = _opponent;
opponents = [[NSMutableArray alloc] init];
NSMutableDictionary *opponent = [[NSMutableDictionary alloc] init];
[opponent setObject:#"opponent A" forKey:#"name"];
[opponent setObject:#"xx matches | xx wins || xx losts" forKey:#"stats"];
[opponent setObject:#"Krems" forKey:#"location"];
NSMutableDictionary *opponent2 = [[NSMutableDictionary alloc] init];
[opponent2 setObject:#"opponent B" forKey:#"name"];
[opponent2 setObject:#"yy matches | yy wins || yy losts" forKey:#"stats"];
[opponent2 setObject:#"Location B" forKey:#"location"];
NSMutableDictionary *opponent3 = [[NSMutableDictionary alloc] init];
[opponent3 setObject:#"opponent C" forKey:#"name"];
[opponent3 setObject:#"zz matches | zz wins || zz losts" forKey:#"stats"];
[opponent3 setObject:#"Location C" forKey:#"location"];
[opponents addObject:opponent];
[opponents addObject:opponent2];
[opponents addObject:opponent3];
[_topponents reloadData];
}
and here my cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"opponentcell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *lopponentname = (UILabel*)[cell viewWithTag:1];
UILabel *lopponentstats = (UILabel*)[cell viewWithTag:2];
UILabel *llocation = (UILabel*)[cell viewWithTag:3];
lopponentname.text = [[opponents objectAtIndex:indexPath.row] objectForKey:#"name"];
lopponentstats.text = [[opponents objectAtIndex:indexPath.row] objectForKey:#"stats"];
llocation.text = [[opponents objectAtIndex:indexPath.row] objectForKey:#"location"];
return cell;
}
I found my fault!
I used to write:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
but I should use
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
with "forIndexPath"
I think, the trouble is in the cell. Create UITableViewCell subclass.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"opponentcell";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.lopponentname.text = [[opponents objectAtIndex:indexPath.row] objectForKey:#"name"];
cell.lopponentstats.text = [[opponents objectAtIndex:indexPath.row] objectForKey:#"stats"];
cell.llocation.text = [[opponents objectAtIndex:indexPath.row] objectForKey:#"location"];
return cell;
}
Hope it helps you.
Add this after initialisation of cell. For me works properly.
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
I think you are using custom cel and it looks you have trouble in "cellForRowAtIndexPath". If you using .xib, you may find it useful.
static NSString *cellIdentifier = #"opponentcell";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray *nibObjects =[[NSBundle mainBundle] loadNibNamed:#"CustomTableViewCell" owner:nil options:nil];
for (id currentObject in nibObjects)
{
if ([currentObject isKindOfClass:[CustomTableViewCell class]])
{
cell = (CustomTableViewCell *) currentObject;
}
}
[cell initViewStyles];
}
You need to check for condition when cell is nil..
Initially the cell is nil..
So the data is loading only after scrolling...
Add below lines of code in your cellForRowAtIndexPath delegate method after initializing cell.
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Hope this fixes the issue...
I want to display in UITableView array like:
(
1,
{
date = 1351876762;
ncom = 0;
nid = 11739814;
"read_ncom" = 0;
text = "<div class=\"wikiText\"><!--4-->New note text </div>";
title = lol;
uid = 3795852;
}
)
Cells should have a label with "title" from this array.
How can I make that?
P.S.:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *textCell = [[array objectAtIndex:indexPath.row] objectForKey:#"title"];
cell.textLabel.text = textCell;
return cell;
}
returns signal SIGABRT.
Thanks.
[EDIT]
Your array seems to be weird. Try following and let me know the result.
NSString *textCell = [[array objectAtIndex:1] objectForKey:#"title"];
You should have cell with identifier "Cell" in your table (if you have storyboard) or create this cell manually
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[array objectAtIndex:1] objectforkey:#"title"];
try the above code
Replace this :
NSString *textCell = [[[array objectAtIndex:indexPath.row] objectForKey:#"1"]objectForKey:#"title"];
I'm having a weird issue - in my code I am fetching data from the internet in XML format and fill a NSMutableArray (altogether 34 items).
This data should be displayed in a TableView. I created a custom TableViewCell for that. However it always only displays the first 10 items (0-9), then the 11th item will change as you scroll up the table (starting from 11 and changing itself to 12, 13, 14 ...), then it will display the first 10 items again.
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
return [myCurrencies count];
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *myIdentifier = #"CurrencyDisplayCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"CurrencyTableViewCell" owner:self options:nil];
cell = tableViewCell;
cell.accessoryType = UITableViewCellAccessoryNone;
self.tableViewCell = nil;
}
NSLog(#"%i",indexPath.row);
actualRate.text = [NSString stringWithFormat:#"%#",[myRates objectAtIndex:indexPath.row]];
currencyCode.text = [NSString stringWithFormat:#"(%i) %#",indexPath.row,[myCurrencies objectAtIndex:indexPath.row]];
countryFlag.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#.gif",[myCurrencies objectAtIndex:indexPath.row]]];
return cell;
}
However when I just set the textlabel of the standard cell everything is fine and I am displayed 34 table rows with numbers 0 - 33:
cell.textLabel.text = [NSString stringWithFormat:#"%i",indexPath.row];
Any idea out there?
your cell initialization is wrong
first of all name all your labels and image views in Interface Builder with tag
for example actualRate - tag 101
currencyCode tag 102
countryFlag tag 103
then
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *myIdentifier = #"CurrencyDisplayCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:#"CurrencyTableViewCell" owner:self options:nil] lastObject];
}
NSLog(#"%i",indexPath.row);
[((UILabel *)[cell viewWithTag:101]) setText:[NSString stringWithFormat:#"%#",[myRates objectAtIndex:indexPath.row]]];
[((UILabel *)[cell viewWithTag:102]) setText:[NSString stringWithFormat:#"(%i) %#",indexPath.row,[myCurrencies objectAtIndex:indexPath.row]]];
[((UIImageView *)[cell viewWithTag:103]) setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#.gif",[myCurrencies objectAtIndex:indexPath.row]]]];
return cell;
}